重写模板类中的派生虚拟方法

问题描述 投票:0回答:1

我有下面的代码,我希望

foo
由派生类实现,但通过
bar
方法在基类中使用。但我需要
foo
来处理两种情况,其中输入和输出类型之一是:

  • 类模板类型
    T
  • bool

类似这样,但这混合了静态和动态多态性。

template <typename R>
virtual container_t<R> foo(const R& value1, const T& value2) const = 0;

但是以下内容不能使用

T = bool
构建,因为调用
foo
变得不明确。我该如何解决这个问题?

#include <iostream>
#include <vector>

template <template <typename> typename container_t, typename T>
class BaseClass {
public:
    virtual container_t<T> foo(const T& value1, const T& value2) const = 0;
    virtual container_t<bool> foo(bool value1, const T& value2) const = 0;

    container_t<T> bar_t() const {
        return foo(T(0), T(0));
    }
    container_t<bool> bar_bool() const {
        return foo(false, T(0));
    }
};

template <typename T>
class DerivedClass : public BaseClass<std::vector, T> {
public:
    using BaseClass<std::vector, T>::bar_t;
    using BaseClass<std::vector, T>::bar_bool;

    DerivedClass() {}

    std::vector<T> foo(const T& value1, const T& value2) const override {
        return std::vector<T>(8, value1);
    }

    std::vector<bool> foo(bool value1, const T& value2) const override {
        return std::vector<bool>(8, value1);
    }
};

int main() {

    DerivedClass<int> int_instance;
    std::cout << int_instance.bar_t().size() << std::endl;
    std::cout << int_instance.bar_bool().size() << std::endl;

    DerivedClass<bool> bool_instance;
    std::cout << bool_instance.bar_t().size() << std::endl;
    std::cout << bool_instance.bar_bool().size() << std::endl;

    return 0;
}

编译器错误:

1>main.cpp
1>main.cpp(15,1): error C2668: 'BaseClass<std::vector,T>::foo': ambiguous call to overloaded function
1>        with
1>        [
1>            T=bool
1>        ]
1>main.cpp(9,31): message : could be 'std::vector<bool,std::allocator<bool>> BaseClass<std::vector,T>::foo(bool,const T &) const'
1>        with
1>        [
1>            T=bool
1>        ]
1>main.cpp(8,28): message : or 'std::vector<bool,std::allocator<bool>> BaseClass<std::vector,T>::foo(const T &,const T &) const'
1>        with
1>        [
1>            T=bool
1>        ]
1>main.cpp(15,1): message : while trying to match the argument list '(bool, T)'
1>        with
1>        [
1>            T=bool
1>        ]
1>main.cpp(14): message : while compiling class template member function 'std::vector<bool,std::allocator<bool>> BaseClass<std::vector,T>::bar_bool(void) const'
1>        with
1>        [
1>            T=bool
1>        ]
1>main.cpp(45): message : see reference to function template instantiation 'std::vector<bool,std::allocator<bool>> BaseClass<std::vector,T>::bar_bool(void) const' being compiled
1>        with
1>        [
1>            T=bool
1>        ]
1>main.cpp(20): message : see reference to class template instantiation 'BaseClass<std::vector,T>' being compiled
1>        with
1>        [
1>            T=bool
1>        ]
1>main.cpp(43): message : see reference to class template instantiation 'DerivedClass<bool>' being compiled
c++ polymorphism virtual-functions
1个回答
0
投票

我该如何解决这个问题?

一种方法是让函数按值获取

bool
,改为通过非
const
引用和右值引用:

virtual container_t<T> foo(const T& value1, const T& value2) const = 0;

// instead of taking the `bool` by value:
virtual container_t<bool> foo(bool& value1, const T& value2) const = 0;
virtual container_t<bool> foo(bool&& value1, const T& value2) const = 0;

...然后你就必须

override
他们两个。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.