我在尝试使用自动模板参数时,惊讶地发现这段代码无法编译:
constexpr auto bar = 2;
template<auto& T>
struct Foo {
auto operator()() const { return T; }
};
int main() {
Foo<bar> b;
b();
}
Visual Studio 15.7(预览版4)会输出以下错误:
error C2970: 'Foo': template parameter 'T': 'bar': an expression involving objects with internal linkage cannot be used as a non-type argument
note: see declaration of 'Foo'
note: see declaration of 'bar'
error C2440: 'specialization': cannot convert from 'int' to 'int &'
note: see reference to class template instantiation 'Foo<0>' being compiled
error C2973: 'Foo': invalid template argument 'int'
note: see declaration of 'Foo'
然后,在添加了 "inline" 后,错误消失了!constexpr inline auto bar = 2;
我以为constexpr
变量是隐式inline
的。此外,这会如何影响我的变量bar
的链接?