Расхождение компилятора при отложенном создании экземпляра шаблона ⇐ C++
-
Anonymous
Расхождение компилятора при отложенном создании экземпляра шаблона
I want to trigger a static_assert, when my base class does not implement a member function.
#include template struct AlwaysFalse : public std::false_type {}; struct B { template void foo(){} }; template struct A:T { auto foo(){ if constexpr (requires{T::template foo();}) return 1; else static_assert(AlwaysFalse::value, "foo is not implemented."); } }; int main() { A a; a.foo(); } This compiles in gcc 12.2 but fails to compile with clang 16 and also clang trunk. If the member function foo is not a template also clang compiles it.
Which compiler is correct here? I suppose GCC, since the base class member function is there and it should not read the AlwaysFalse. Is my code even standard compliant c++?
I also have a workaround, by creating an object in the requires expression
if constexpr (requires(T t){t. template foo();}) which compiles again for both compilers.
Templated version
Non-templated version
Workaround
Источник: https://stackoverflow.com/questions/781 ... tantiation
I want to trigger a static_assert, when my base class does not implement a member function.
#include template struct AlwaysFalse : public std::false_type {}; struct B { template void foo(){} }; template struct A:T { auto foo(){ if constexpr (requires{T::template foo();}) return 1; else static_assert(AlwaysFalse::value, "foo is not implemented."); } }; int main() { A a; a.foo(); } This compiles in gcc 12.2 but fails to compile with clang 16 and also clang trunk. If the member function foo is not a template also clang compiles it.
Which compiler is correct here? I suppose GCC, since the base class member function is there and it should not read the AlwaysFalse. Is my code even standard compliant c++?
I also have a workaround, by creating an object in the requires expression
if constexpr (requires(T t){t. template foo();}) which compiles again for both compilers.
Templated version
Non-templated version
Workaround
Источник: https://stackoverflow.com/questions/781 ... tantiation
Мобильная версия