Расхождение компилятора при отложенном создании экземпляра шаблона ⇐ C++
Расхождение компилятора при отложенном создании экземпляра шаблона
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
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Woocommerce показывает пользовательское сообщение об отложенном заказе при обновлении корзины
Anonymous » » в форуме Php - 0 Ответы
- 49 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Проблемы компилятора Cygwin gcc с созданием экземпляра шаблона с использованием GAlib
Anonymous » » в форуме C++ - 0 Ответы
- 8 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Woocommerce – отключить кнопку add_to_cart, когда товар находится в отложенном заказе
Anonymous » » в форуме Php - 0 Ответы
- 25 Просмотры
-
Последнее сообщение Anonymous
-