【问题标题】:Problem with expanding using-declaration with C++17使用 C++17 扩展使用声明的问题
【发布时间】:2019-09-11 16:31:36
【问题描述】:

我试图通过 C++17 中的包扩展了解一些新的可能性,但遇到了一个我不明白的问题。 我创建了一个继承自一组基类模板的派生类。

然后我尝试将所有基类构造函数导入并添加一个访问基类常量的方法。 但是,当我尝试编译时出现以下错误。

error: expected primary-expression before '...' token    23 |      
return mValue == Base<M, tuple_element_t<Is, Tuple>...>::value

如果我注释基类构造函数的导入或注释方法,编译工作会发现。 有什么东西让我不能两者兼得吗?

这是一个最小的例子:

#include <tuple>
#include <utility>
using namespace std;

template <size_t N, typename... Types>
struct Base
{
    static constexpr size_t value = N;
};

template <typename Tuple, typename Indices>
struct Derived;

template <typename Tuple, size_t... Is>
struct Derived<Tuple, index_sequence<Is...>> : public Base<Is, std::tuple_element_t<Is,Tuple>... >...
{   
    // Works if following line is commented
    using Base<Is, tuple_element_t<Is, Tuple>...>::Base...;

    template<size_t M> bool check() const
    {
      return mValue == Base<M, tuple_element_t<Is, Tuple>...>::value;
    }

    size_t mValue;
};

template <typename... Types>
struct Test : public Derived<tuple<Types...>, make_index_sequence<sizeof...(Types)>>
{};

int main ()
{
    using MyType = Test<int, float, float>;
    MyType test;

    return 0;
}

【问题讨论】:

    标签: c++ templates c++17 template-meta-programming


    【解决方案1】:

    这是gcc bug 79094。该程序格式良好,clang和icc接受它。

    msvc 接受 gcc 错误报告中的简单版本,但拒绝这个稍微复杂的版本(与您的示例更相似):

    template <typename>
    class U { };
    
    template <class... Args>
    struct X : U<Args>... 
    {
        using U<Args>::U...;
    }; 
    

    【讨论】:

    • 非常感谢。我以为我疯了。我在发布前检查了最新版本的 wandbox,但再次选择了 gcc。
    • @the_summer 是的,做我们在此功能之前所做的事情 - 拥有一个线性继承链并一次一个地引入每个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 2018-03-28
    相关资源
    最近更新 更多