次のコードを検討してください。
struct Foo
{
const char str[] = "test";
};
int main()
{
Foo foo;
}
g ++とclang ++の両方でコンパイルに失敗し、本質的に吐き出します
error: array bound cannot be deduced from an in-class initializer
これはおそらく標準が言っていることだと思いますが、それには何か特別な理由がありますか? const
文字列リテラルがあるため、クラス外のC のような null で終了する文字列を宣言する場合と同様に、コンパイラは問題なくサイズを推測できるようです。