【发布时间】:2023-01-11 04:04:58
【问题描述】:
如何编译以下代码?
我正在尝试检查 BigStruct 是否存在于某个类型中,如果存在,则启用 f。
#include <type_traits>
struct A {
using BigStruct = int;
};
struct C {
};
template <typename T>
struct B {
void f(typename T::BigStruct t) requires requires {T::BigStruct;} {}
};
int main() {
B<A> b1;
B<C> b2;
}
我得到的错误:
<source>:11:24: error: no type named 'BigStruct' in 'C'
void f(typename T::BigStruct t) requires requires {T::BigStruct;} {}
~~~~~~~~~~~~^~~~~~~~~
<source>:16:8: note: in instantiation of template class 'B<C>' requested here
B<C> b2;
^
1 error generated.
ASM generation compiler returned: 1
<source>:11:24: error: no type named 'BigStruct' in 'C'
void f(typename T::BigStruct t) requires requires {T::BigStruct;} {}
~~~~~~~~~~~~^~~~~~~~~
<source>:16:8: note: in instantiation of template class 'B<C>' requested here
B<C> b2;
^
1 error generated.
Execution build compiler returned: 1
【问题讨论】:
-
你得到什么错误?
-
B是类模板是故意的吗?或者您可能只是想让f成为一个函数模板? -
@tadman 不编译,“‘C’中没有名为‘BigStruct’的类型”
-
@463035818_is_not_a_number B 是一个类模板是故意的。
-
如今,错误消息包含了如此多的信息。使用它们。在问题中包含完整的错误消息。
标签: c++ c++20 c++-concepts