c++ - 这是关于 C++ 中的 <concept> 库的 BUG 吗?

标签 c++ c++-concepts

我在学习 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/

相关文章:

c++ - 错误 : no match for ‘operator==’ in ‘boiler::uniqueInstance == 0l’

c++ - 库构建器的预编译头文件用法

c++ - 在 C++ 中预热一些变量

c++ - C++ 中的概念检查变化?

c++ - 在 if constexpr 中使用带有参数包的概念时升级到 gcc 9 后出现编译错误

C++ 2a - 多态范围

c++ - shared_ptr 的问题。我无法在没有错误的情况下创建我的结构

c++ - 如何仅为函数指针编写正确的构造函数?

c++ - 当模板类 is_convertible 为众所周知的类型时,特化仿函数

c++ - concepts(C++20) 可以用作 bool 值吗?