VC 2013:使用声明重新定义成员函数会导致编译错误

我想允许通过指定策略来​​修改我的类的行为.该策略应该用作boost :: variant的访问者.默认策略适合大多数情况良好,但用户可能需要添加或替换一些重载.

我发现vc 2013没有使用错误C3066编译此代码:有多种方法可以使用这些参数调用此类型的对象.相同的代码在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这个exaple适用于vc 2010.看起来它是2013版本的bug.

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;
}

最佳答案 代码格式正确. 7.3.3 / 15:

When a using-declaration brings names from a base class into a derived class scope, member functions and member function templates in the derived class override and/or hide member functions and member function templates with the same name, parameter-type-list (8.3.5), cv-qualification, and ref-qualifier (if any) in a base class (rather than conflicting).

所以UserModifiedPolicy :: operator()(int)仍然应该隐藏DefaultPolicy :: operator()(int).而operator()的名称查找应该找到三个成员DefaultPolicy :: operator()(bool),UserModifiedPolicy :: operator()(int)和UserModifiedPolicy :: operator()(float).

点赞