文字列リテラルのテンプレート特殊化を追加しようとしている間、VS 2017コンパイラとVS 2010コンパイラの動作の違いに気付きましたVS 2017から)(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