在嘗試爲字符串文字添加模板專業化時,我注意到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