1

一意性のための可変個引数テンプレート パラメータの確認についてここに投稿します 。一意性のための可変個引数テンプレート パラメータを確認してください。

私の質問: 次のコードがコンパイルされないのはなぜですか? それはバグコンパイラですか、それとも標準は許可されていませんか?

#include <iostream>


template< class ... > struct pack{};
template< class  > struct id{};
template< class  > struct base_all;
template< class ...T>
struct base_all< pack<T...> > : id<T> ... {using type = int;}; // <-- error with `int`, `char`, `int`  parameters.

template< class ...T>
struct is_unique
{
    template< class P,  std::size_t = sizeof(base_all<P>)  >
    struct check;

    template< class P >
    static constexpr bool test( check< P > * ) noexcept { return true ;}

    template< class P >
    static constexpr bool test( ... ) noexcept{ return false; }

    static constexpr bool value = test< pack<T...> >( nullptr );
};

int main()
{
    constexpr bool b = is_unique<int, float, double>::value;

    constexpr bool c = is_unique<int, char, int >::value; //<--- error 

    std::cout << std::boolalpha << "b = " << b << "\nc = " << c << std::endl;
}

エラー コンパイラ gcc 4.8.1:

is_unique_args.cpp:16:42:   required by substitution of ‘template<class P> static constexpr bool is_unique<T>::test(is_unique<T>::check<P>*) [with P = P; T = {int, char, int}] [with P = pack<int, char, int>]’
4

1 に答える 1

1

私はあなたの例を次のようにコンパイルしました:

g++ -Wall -Wextra -std=c++11 -rdynamic -pedantic garbage.cpp

別のエラーが発生しました:

garbage.cpp: In instantiation of ‘struct base_all<pack<int, char, int> >’:
garbage.cpp:17:27:   required by substitution of ‘template<class P> static constexpr bool is_unique::test(is_unique<T>::check<P>*) [with P = P; T = {int, char, int}] [with P = pack<int, char, int>]’
garbage.cpp:22:63:   required from ‘constexpr const bool is_unique<int, char, int>::value’
garbage.cpp:29:52:   required from here
garbage.cpp:8:8: error: duplicate base type ‘id<int>’ invalid

エラーをもう一度強調表示するには:

重複するベース タイプ 'id' が無効です

それが何を意味するかは非常に明確です。c++ では、同じ型の基底クラスを複数持つことは禁止されています。したがって、これは標準で禁止されています:

struct A
{};
struct B : A, A
{};

そして、それがあなたが上でやろうとしたことです。

于 2013-09-27T08:59:09.687 に答える