在学习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.
...这是一个错误,还是我不应该在requires-expression的参数列表中使用Test<typename T::type>?
注意:我不能报告这个bug,因为在buggzilla上创建帐号是有限制的。
发布于 2019-07-28 00:34:39
任何内部编译器错误都是事实上的编译器错误。问题是,如果编译器工作正常,你的代码应该是有效的吗?
不是的。
Test<typename T::type>是一个constexpr布尔变量,它最终归结为值true。并且变量不是requires表达式参数内的合法typename。
您希望在参数中使用的只是typename T::type,因为这是您希望提供给sth的类型。但您还希望将此概念限制在具有::type typename成员的T中。
基本上,您不需要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) };
};我认为你陷入了前概念思维的陷阱,你试图使用通常的模板元编程解决方案,而不是概念解决方案。尽可能直接地表达你想要的需求。
https://stackoverflow.com/questions/57233950
复制相似问题