我想允许通过指定策略来修改我的班级的行为。此策略应用作 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;
}