1
C++ 11で練習するために、私はバリデーションテンプレートを使って遊んでいます。g ++とclang ++バリデーションコンテナとの動作が異なります
特に、再帰的なバリデーションコンテナクラス(onion
)と、テンプレートタイプ(func()
)の数を返す関数を使用しています。
g ++(4.9.2)が問題なくコンパイルされて実行されている間に、clang ++(3.5.0)がコンパイルできない場合がありました。
#include <iostream>
template <typename F, typename ... O>
struct onion;
template <typename F, typename S, typename ... O>
struct onion<F, S, O...>
{
F first;
onion<S, O...> others;
};
template <typename L>
struct onion<L>
{ L last; };
template <typename ... Args>
std::size_t func (onion<Args...> const &)
{ return sizeof...(Args); }
int main()
{
auto o = onion<int, char const *, float> { 23, { "abcd", {34.0f}} };
std::cout << func(o) << '\n';
return 0;
}
打ち鳴らす++(3.5.0)を以下のように私はそれを単純化している
は私に次のコンパイラエラーを与える
test.cpp:28:17: error: no matching function for call to 'func'
std::cout << func(o) << '\n';
^~~~
test.cpp:20:13: note: candidate template ignored: substitution
failure [with Args = <>]: too few template arguments for class template
'onion'
std::size_t func (onion<Args...> const &)
^ ~~~~~
1 error generated.
グラム++(4.9.2)問題なくコンパイルし、実行し、出力3
。
私の質問は誰ですか?
clang ++、エラーを返すか、g ++、コンパイルしていますか?
私はこのよう
template <typename X, typename ... Args>
std::size_t func (onion<X, Args...> const &)
{ return 1U + sizeof...(Args); }
または私は
template <typename ... O>
struct onion;
このようにonion
を再定義する場合は、両方の打ち鳴らす++およびgはエラーなくコンパイル++でfunc()
を書き換える場合、私はそれを追加します。
clang ++ 3.7.0がコンパイルされます。 – kec
あなたの英語は上手です。 (私が謝罪するまであなたがネイティブスピーカーではないことを示唆するものは何も気付かなかった。) –