在尝试为字符串文字添加模板专业化时,我注意到VS 2017编译器和VS 2010编译器之间的行为差异从2017年VS)(Visual-)用于字符串文字的C++模板类型推断 - VS 2010 vs VS 2017
这是有问题的代码:使用默认的编译器VS 2017年
#include <iostream>
template <typename T>
struct foo
{
static const int a = -1;
};
template <size_t n>
struct foo<char[n]>
{
static const int a = 0;
};
template <size_t n>
struct foo<const char[n]>
{
static const int a = 1;
};
template <typename T>
int bar(const T& x)
{
#pragma message (__FUNCSIG__)
return foo<T>::a;
}
int main()
{
std::cout << _MSC_VER << '\n';
std::cout << bar("a") << '\n';
}
运行:
使用VS 2010编译器int __cdecl bar<char[2]>(const char (&)[2]) 1911 0
并运行:
int __cdecl bar<const char[2]>(const char (&)[2]) 1600 1
正如你所看到的,T
被推断为const char[2]
旧编译器,但char[2]
为新的。什么改变了?这是Visual Studio中的错误修正/错误还是在C++ 11/C++ 14中改变了正确的行为?
试着用tio.run(gcc和clang),似乎VS 2017是正确的,这是正确的吗?
FWIW,C++中的字符串文字一直是'const char [N]',但如果VS为了C兼容性而将它们作为'char [N]',这是可以理解的。 – chris