【问题标题】:Single type compile time list: concat with clang单一类型编译时间列表:concat 与 clang
【发布时间】:2016-12-02 10:54:27
【问题描述】:

我正在为我的一个项目使用“固定类型编译时列表”。最近我测试了这个项目与不同编译器的兼容性,我注意到 clang (3.8) 无法编译我的实现。 出现了这个错误:

error: expected expression return

List<T, sizeof...(Ints1) + sizeof...(Ints2)>(this->get<Ints1>()..., rhs.get<Ints2>()...);                                                          
                                                                                   ^

以下部分摘自我对编译时间列表的实现:

template<class T, size_t TNum>
class List;

template<class T, size_t TNum>
class List: public List<T, TNum - 1>
{
protected: 
    T data;
    template<size_t ... Ints1, size_t ... Ints2>
    constexpr List<T, sizeof...(Ints1) + sizeof...(Ints2)> _concat(const List<T, sizeof...(Ints2)> rhs, std::index_sequence<Ints1...>, std::index_sequence<Ints2...>) const
    {
        return List<T, sizeof...(Ints1) + sizeof...(Ints2)>(this->get<Ints1>()..., rhs.get<Ints2>()...);
    }

    template<class ... TArgs>
    constexpr List(T d, TArgs&& ... arg)
        : List<T, TNum - 1>(std::forward<TArgs>(arg)...), data(d)
    {
        static_assert(TNum != sizeof...(TArgs), "Number of arguements and list size does not match!");
    }        

    template<size_t TNum2, typename Indices1 = std::make_index_sequence<TNum>, typename Indices2 = std::make_index_sequence<TNum2>>
    constexpr List<T, TNum + TNum2> concat(const List<T, TNum2>& rhs) const
    {
        return this->_concat(rhs, Indices1(), Indices2());
    }

    template<size_t TI>
    constexpr T get() const
    {
        static_assert(TI < TNum, "Element out of valid range!");
        static_assert(TI >= 0, "Element out of valid range!");
        return static_cast<List<T, TNum - TI> >(*this).get();
    }
};

此外,此示例中缺少 TNum=1 和 TNum=0 的两个特殊化。如果需要,我可以添加它们

希望您能帮我找出造成此问题的错误

编辑: 感谢 Jarod42 的回答。在他的帮助下,我发现了这个:Where and why do I have to put the "template" and "typename" keywords? 这进一步解释了事情。

【问题讨论】:

  • 您可能在编译器指定的位置get 之前缺少模板关键字...

标签: c++ templates variadic-templates constexpr compile-time


【解决方案1】:

templaterhs.get&lt;Ints2&gt;() 中丢失:

应该是

rhs.template get<Ints2>()

Demo

【讨论】:

  • 谢谢,这很好用。不知道为什么 VC++ 和 gcc 没有这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
  • 1970-01-01
  • 2016-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多