我在学习 C++ 中的概念库时遇到了“内部编译器错误”...
环境:
编译命令:g++ -std=c++17 test.cpp -fconcepts -g -v| more;
一些编译输出:
Thread model: posix
gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
COLLECT_GCC_OPTIONS='-std=c++17' '-fconcepts' '-g' '-v' '-save-temps' '-
shared-libgcc' '-mtune=core2' '-march=nocona'
我的代码:
template<class A, class B>
concept bool Test = true;
template<class T>
concept bool Ohh = requires(T t, Test<typename T::type> sth){
{ t.func(sth) };
};
//this one works well !!
// template<class T>
// concept bool OK = requires(T t){
// { t.func(Test<typename T::type>) };
// };
template<class T>
struct A{
typedef T type;
void func(T){}
};
Ohh{T} /* OK{T} works fine */
struct B{
static const bool value = true;
};
int main(int argc, char *argv[] /*, char *envp[]*/)
{
cout << B<A<int>>::value;
}
错误消息如下:
internal compiler error: in synthesize_implicit_template_parm, at cp/parser.c:39068
concept bool Ohh = requires(T t, Test<typename T::type> sth){
^
libbacktrace could not find executable to open.
...
这是一个错误还是我不应该使用 Test<typename T::type>
在 requires-expression 的参数列表中?
注意:我无法报告此错误,因为帐户创建在 buggzilla 上受到限制。
最佳答案
任何内部编译器错误都是事实上的编译器错误。问题是,如果编译器工作正常,您的代码是否有效?
没有。
Test<typename T::type>
是 constexpr
bool 变量,最终归结为值 true
.变量在 requires
的参数中不是合法的类型名表达。
你想要的参数就是typename T::type
,因为那是你想要给的类型 sth
.但是您还想要的是将此概念限制为 T
它有一个 ::type
typename 成员。
基本上,您不需要 Test
:
template<class T>
requires requires { T::type; }
concept bool Ohh = requires(T t, typename T::type sth){
{ t.func(sth) };
};
或者,如果您想将具有 ::type
的类型概念化类型名称:
template<class T>
concept bool HasType = requires { T::type; };
template<HasType T>
concept bool Ohh = requires(T t, typename T::type sth){
{ t.func(sth) };
};
我认为您陷入了先入为主的思维陷阱,您已经尝试使用通常的模板元编程解决方案而不是概念一。尽可能直接地表达你想要的需求。
关于c++ - 这是关于 C++ 中的 <concept> 库的 BUG 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57233950/