首页 > 解决方案 > 为什么我得到静态双变量的“变量的实例化......这里需要,但没有可用的定义”,而不是int?

问题描述

我正在编译这个程序:

template<int N> struct Foo {
  static const double value;
};

int main() {
  auto const& value = Foo<1>::value;
}

编译器输出头:

$ clang++ -std=c++17 ./main.cpp  -o main
./main.cpp:6:31: warning: instantiation of variable 'Foo<1>::value' required here, but no definition is available [-Wundefined-var-template]
  auto const& value = Foo<1>::value;
                              ^
./main.cpp:2:23: note: forward declaration of template entity is here
  static const double value;
                      ^
./main.cpp:6:31: note: add an explicit instantiation declaration to suppress this warning if 'Foo<1>::value' is explicitly instantiated in another translation unit
  auto const& value = Foo<1>::value;

神箭:https ://godbolt.org/z/V5-2Lh

如果我更改doubleint,该警告就会消失。

在 C++ 规范中是否有这样做的原因,或者这个 LLVM 是否不一致?

标签: c++llvm

解决方案


推荐阅读