首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这是关于c++中的<concept>库的错误吗?

这是关于c++中的<concept>库的错误吗?
EN

Stack Overflow用户
提问于 2019-07-28 00:18:48
回答 1查看 162关注 0票数 3

在学习c++中的概念库时,我遇到了一个“内部编译器错误”...

环境:

编译命令:g++ -std=c++17 test.cpp -fconcepts -g -v| more;

一些编译输出:

代码语言:javascript
复制
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'

我的代码:

代码语言:javascript
复制
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;
}

以下是错误消息:

代码语言:javascript
复制
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上创建帐号是有限制的。

EN

回答 1

Stack Overflow用户

发布于 2019-07-28 00:34:39

任何内部编译器错误都是事实上的编译器错误。问题是,如果编译器工作正常,你的代码应该是有效的吗?

不是的。

Test<typename T::type>是一个constexpr布尔变量,它最终归结为值true。并且变量不是requires表达式参数内的合法typename。

您希望在参数中使用的只是typename T::type,因为这是您希望提供给sth的类型。但您还希望将此概念限制在具有::type typename成员的T中。

基本上,您不需要Test

代码语言:javascript
复制
template<class T>
    requires requires { T::type; }
concept bool Ohh = requires(T t, typename T::type sth){
    { t.func(sth) };
};

或者,如果您想要概念化具有::type类型名称的类型的概念:

代码语言:javascript
复制
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) };
};

我认为你陷入了前概念思维的陷阱,你试图使用通常的模板元编程解决方案,而不是概念解决方案。尽可能直接地表达你想要的需求。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57233950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档