C ++ 14的静态constexpr自动带ODR使用

问题描述 投票:4回答:1

我有以下C ++ 14代码:

template<typename T>
struct Test{
    static constexpr auto something{T::foo()};
};

这是完全正常的,只要T::foo()constexpr为好。

现在我有something是ODR使用,所以我需要提供一个命名空间声明。我应该使用什么语法?

template<typename T>
constexpr auto Test<T>::something;

不工作。谢谢!

c++ templates c++14 constexpr auto
1个回答
2
投票

怎么样通过一个using定义的类型名称?

template <typename T>
struct Test
 {
   using someType = decltype(T::foo());

   static constexpr someType something{T::foo()};
 };

template<typename T>
constexpr typename Test<T>::someType Test<T>::something;
© www.soinside.com 2019 - 2024. All rights reserved.