gpt4 book ai didi

c++14 static constexpr auto 与 odr 用法

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:06:43 25 4
gpt4 key购买 nike

我有以下 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;

不起作用。谢谢!

最佳答案

通过 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;

关于c++14 static constexpr auto 与 odr 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54590141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com