const 指针专业化使用 MSVC 生成未解析的符号并且可以使用 GCC/Clang
const pointer specialization produces unresolved symbol with MSVC and is fine with GCC/Clang
请看下面的代码:
// header.h
#pragma once
template <typename T>
void test(T*);
// lib.cpp
#include "header.h"
template <>
void test(int* const)
{
}
// main.cpp
#include "header.h"
int main() {
int* ptr = nullptr;
test(ptr);
}
gcc 和 clang 都编译并且 link 它没有任何问题,而 MSVC(2015、2019)产生未解析的外部符号 "void __cdecl test(int *)"
请注意,如果从 lib.cpp 中删除 const 关键字,那么 MSVC link 的代码就可以了。
谁在这儿?是MSVC的bug吗?
此程序格式错误,无需诊断。
[temp.expl.spec]/6 If a template, a member template or a member of a class template is explicitly specialized then that specialization shall be declared before the first use of that specialization that would cause an implicit instantiation to take place, in every translation unit in which such a use occurs; no diagnostic is required.
请看下面的代码:
// header.h
#pragma once
template <typename T>
void test(T*);
// lib.cpp
#include "header.h"
template <>
void test(int* const)
{
}
// main.cpp
#include "header.h"
int main() {
int* ptr = nullptr;
test(ptr);
}
gcc 和 clang 都编译并且 link 它没有任何问题,而 MSVC(2015、2019)产生未解析的外部符号 "void __cdecl test(int *)"
请注意,如果从 lib.cpp 中删除 const 关键字,那么 MSVC link 的代码就可以了。
谁在这儿?是MSVC的bug吗?
此程序格式错误,无需诊断。
[temp.expl.spec]/6 If a template, a member template or a member of a class template is explicitly specialized then that specialization shall be declared before the first use of that specialization that would cause an implicit instantiation to take place, in every translation unit in which such a use occurs; no diagnostic is required.