【发布时间】:2014-06-25 12:56:53
【问题描述】:
我想允许通过指定策略来修改我的班级的行为。此策略应用作 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的bug吗?
#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;
}
【问题讨论】:
-
哪个调用产生了错误?
-
什么版本的vc++?
-
UserModifiedPolicy()(1); // 此处出错(int 重载)
标签: c++ visual-c++-2013