使用可变参数概念模板时的gcc内部错误

问题描述 投票:3回答:2

我最近在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.
c++ gcc
2个回答
0
投票

这是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) { /*...*/ }
};

0
投票

与@nyronium的答案类似,但更简单一点,您可以在requires子句中使用fold表达式。

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

我测试了GCC 7.3.0上的等价物。

© www.soinside.com 2019 - 2024. All rights reserved.