4

ポリシーを指定して、クラスの動作を変更できるようにしたいと考えています。このポリシーは、boost::variant の訪問者として使用する必要があります。ほとんどの場合に適したデフォルトのポリシーがありますが、ユーザーはいくつかのオーバーロードを追加または置換する必要がある場合があります。

vc++ 2013 がこのコードをエラーでコンパイルしないことがわかりましたC3066: there are multiple ways that an object of this type can be called with these arguments。gcc と clang で同じコードがコンパイルされ、期待どおりに動作します。

vc++ 2013 のバグですか?

#include <iostream>

struct DefaultPolicy
{
    void operator()( bool ) { std::cout << "Base: bool" << std::endl; }
    void operator()( int ) { std::cout << "Base: int" << std::endl; }
};

struct UserModifiedPolicy : public DefaultPolicy
{
    using DefaultPolicy::operator();
    void operator()( int ) { std::cout << "Derived: int" << std::endl; }
    void operator()( float ) { std::cout << "Derived: float" << std::endl; }
};

int main()
{
    UserModifiedPolicy()(true);
    UserModifiedPolicy()(1); // <-- ERROR HERE
    UserModifiedPolicy()(1.f);
    return 0;
}

UPDこの例は vc++ 2010 で動作します。2013 バージョンのバグのようです。


UPD の回避策

#include <iostream>

struct DefaultPolicy
{
    void operator()( bool ) { std::cout << "Base: bool" << std::endl; }
    void operator()( int ) { std::cout << "Base: int" << std::endl; }
};

struct UserModifiedPolicy : public DefaultPolicy
{
    // Using template to forward a call to the base class:
    template< class T >
    void operator()( T && t ) { DefaultPolicy::operator()( std::forward<T>(t) ); }

    void operator()( int ) { std::cout << "Derived: int" << std::endl; }
    void operator()( float ) { std::cout << "Derived: float" << std::endl; }
};

int main()
{
    UserModifiedPolicy()(true);
    UserModifiedPolicy()(1);
    UserModifiedPolicy()(1.f);
    return 0;
}
4

1 に答える 1

2

コードは整形式です。7.3.3/15:

using-declarationが基底クラスの名前を派生クラスのスコープに持ち込む場合、派生クラスのメンバー関数とメンバー関数テンプレートは、同じ名前のパラメーター型リスト (8.3 .5)、cv-qualification、およびref-qualifier (存在する場合) を基底クラスに (競合するのではなく) 使用します。

だからUserModifiedPolicy::operator()(int)まだ非表示にする必要がありますDefaultPolicy::operator()(int)。の名前検索では、 、、およびoperator()の 3 つのメンバーが検出されます。DefaultPolicy::operator()(bool)UserModifiedPolicy::operator()(int)UserModifiedPolicy::operator()(float)

于 2014-06-25T13:15:54.630 に答える