根据这个answer constexpr 函数始终为 inline
.
可以声明模板函数constexpr
即使所有特化都不能满足 constexpr
的要求功能。在最后一种情况下,特化不是 constexpr
.
例如:
template<class T>
constexpr decltype(auto) size(const T& a){
return a.size();
}
std::array<int,10> arr;
std::vector<int> vec;
size(arr);//constexpr
size(vec);//not a constexpr;
实例化size<std::vector>
不是constexpr
,但是是inline
吗? ?
最佳答案
是的;引用 N4640,[dcl.constexpr]/1:
… A function or static data member declared with the
constexpr
specifier is implicitly an inline function or variable. …
这里的关键是“声明为”——声明才是重要的,而不是满足 constexpr
要求。
关于c++ - 即使实例化不是 constexpr,声明为 constexpr 的模板函数是否始终内联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42834902/