c++ - 使用可变参数概念模板时出现 gcc 内部错误

标签 c++ gcc

我最近研究了 gcc 中的概念功能,在类的构造函数或成员函数中使用可变参数概念模板时偶然发现了这个错误:

template<typename From, typename To>
concept bool ConvertibleNoNarrow = requires(From f, To t) {
    t = { f };
};

class Foo
{
public:
    template<ConvertibleNoNarrow<double>... Args>
    Foo(Args&&... args) { /*...*/ }
};

使用 Foo 时,gcc 显示内部错误:

err.cpp: In substitution of ‘template<class ... Args>  requires  ConvertibleNoNarrow<Args, double>... Foo::Foo(Args&& ...) [with Args = {double}]’:
err.cpp:23:11:   required from here
err.cpp:13:3: internal compiler error: in tsubst_constraint, at cp/constraint.cc:1956
   Foo(Args&&... args) { }
   ^~~

如果在全局函数中使用相同的签名,一切都会按预期工作:

/* works */    
template<ConvertibleNoNarrow<double>... Args>
void Test(Args&&... args) { }

任何人都可以重现这个或知道为什么会发生这种情况以及如何调用现有的错误报告吗?

编辑:

我的 gcc 版本:

gcc (Gentoo 7.2.0 p1.1) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

最佳答案

这是 gcc ( bugzilla link ) 中的错误。

不过,您可以通过在 requires 子句中添加约束来解决该问题。因此,我们检查每种类型的概念并相应地返回 std::true_typestd::false_type。通过这样做,我们可以使用 std::conjunction_v 和参数包扩展来独立约束每个类型。

class Foo
{
public:
    template<typename... Args>
        requires std::conjunction_v<std::conditional_t
            < ConvertibleNoNarrow<Args, double>
            , std::true_type
            , std::false_type
            >...>
    Foo(Args&&... args) { /*...*/ }
};

关于c++ - 使用可变参数概念模板时出现 gcc 内部错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47423721/

相关文章:

c - 对 'funtion_name' 的 undefined reference

c++ - 使用 boost C++ 单元测试套件测试非 fatal error 消息

c++ - 快速选择算法的最小 kth

c++ - 即时推导

c++ - 使用 iostream 堆栈内存

c++ - 从有限列表中简单选择

java - 生成 jpy `gcc: error: : No such file or directory` 时出错

GCC 链接顺序改变了?

linux - GCC 4.7.2 需要 ppl?

c - Linux 内核中的 likely()/unlikely() 宏存在段错误