ポリシーを指定して、クラスの動作を変更できるようにしたいと考えています。このポリシーは、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;
}