未定义的引用指针的变量模板在clang中功能,而不是GCC
undefined reference to variable template of pointer to function in clang but not gcc
#include <iostream>
static constexpr bool isSSE2 = true;
template<typename T>
static void (*fp)();
template<typename T>
static void foo_c() {
std::cout << "foo_c get called." << std::endl;
}
template<typename T>
static void foo_sse2() {
std::cout << "foo_sse2 get called." << std::endl;
}
int main() {
if (isSSE2)
fp<int> = foo_sse2<int>;
else
fp<int> = foo_c<int>;
fp<int>();
return 0;
}
i有一个使用变量模板的项目,该模板本身就是用于功能的指针。上面的示例代码在GCC 6.3中编译并执行正常,但在Clang 3.9.1中给出警告和错误。
。$ clang++ "Source.cpp" -o "foo.exe" -std=c++14 -O2
Source.cpp:6:15: warning: variable 'fp<int>' has internal linkage but is not defined [-Wundefined-internal]
static void (*fp)();
^
Source.cpp:20:9: note: used here
fp<int> = foo_sse2<int>;
^
1 warning generated.
C:msys64tmpSource-6600e8.o:(.text+0x2a): undefined reference to `fp<int>'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
任何帮助都将不胜感激。
首先必须初始化fp&lt;>():
template<typename T>
static void (*fp)() = nullptr;
它在Clang 4.0:示例代码中编译并运行良好。并尝试始终初始化您的变量 - 它可能会使您免于各种头痛。:)
相关文章:
- 具有sleep_for和sleep_until功能的 gcc 可能存在的问题
- 是否可以在gcc中只启用特定的C++语言功能
- AVR-GCC:(看似)简单功能中不需要的序言/尾声
- 为什么 GCC 没有为我决定内联或不内联这个功能?
- C 11螺纹 - 非静态成员功能的使用无效 - 工作GCC 5.1.0损坏的GCC 7.3.1
- GCC / DIAB:保持未使用的功能 /部分
- 模板类中的静态功能:GCC与VS
- 为什么GCC违反了调用ABS功能的代码,并用简短的参数进行功能
- 如何求解与GCC/Clang不同的C 功能模板的MSVC处理(无法识别的模板定义)
- GCC称将功能与多个继承过载时,称其为模棱两可,但Clang和MSVC没有
- 未定义的引用指针的变量模板在clang中功能,而不是GCC
- 从GCC功能树节点中检索函数参数
- C++11 具有与不同版本的 GCC 兼容的功能
- 如何获得GCC版本支持特定功能
- 将GCC功能属性放置在自动功能中
- GCC vs Clang:将功能与-FPIC一起插入
- GCC 是否可以修改 C++ 功能的 C 标头
- 在PowerPC编译器上删除功能,而不是GCC上的功能
- C GCC功能模板超载编译器问题
- 要使 gcc 4.8.1 支持 c++11 中的新"auto"功能?