即使实例化不是 constexpr,模板函数声明的 constexpr 是否总是内联?

Does template function declared constexpr allways inline even if instantiation is not constexpr?

根据这个 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要求的满足。