Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22002

Should a definition inside template class be instantiated if it is not used?

$
0
0
template <typename T>struct A{    static constexpr T obj {};    static constexpr bool noexcept_copy = noexcept( T{obj} );    static void UsesCopy() { T{obj}; }    static constexpr int  C = 1;};struct NoCopy{    constexpr NoCopy() = default;    NoCopy(const NoCopy&) = delete;};int main(){    return A<NoCopy>::C;}

The code above is successfully compiled by GCC, but Clang gives a compilation error:

tmp.cpp:6:57: error: call to deleted constructor of 'NoCopy'        static constexpr bool noexcept_copy = noexcept( T{obj} );                                                        ^~~~~~tmp.cpp:20:16: note: in instantiation of template class 'A<NoCopy>' requested here        return A<NoCopy>::C;               ^tmp.cpp:15:9: note: 'NoCopy' has been explicitly marked deleted here        NoCopy(const NoCopy&) = delete;        ^1 error generated.

The A::UsesCopy function uses copy constructor as well, but the compiler does not complain about the usage of deleted function there. What is the difference between UsesCopy function and noexcept_copy constexpr? Both use copy constructor of NoCopy class and both are not used but the constexpr definition produces a compilation error, the function definition does not.

PS. Clang compiles the code above with -std=c++17 or -std=c++2a, but not with -std=c++11 or -std=c++14.


Viewing all articles
Browse latest Browse all 22002

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>