【问题标题】:Specialize base class with template template parameter使用模板模板参数专门化基类
【发布时间】:2018-05-17 10:59:50
【问题描述】:

我把头撞在下面的代码上:

template<class... Ts> struct typelist{};

template<class S> struct Fun;

template<template<class...> class S, class... Ts>
struct Fun<S<Ts...>> {
    std::tuple<Ts...> _fun;
};

template<class S, class P> struct Gun;

template<template<class...> class S, class... Ts, class P>
struct Gun<S<Ts...>, P>: Fun<S<Ts...>>{
    auto hun(){
        std::cout << std::get<0>(_fun); // error: use of undeclared identifier '_fun'
    }
};

auto main(int /*argc*/, char* /*argv*/[])-> int {
    auto gun = Gun<typelist<int>, float>{};
    gun.hun();
    return 0;
}

我不明白这里发生了什么以及为什么会出现该错误。一定有什么明显的我没看到……

【问题讨论】:

    标签: c++ templates inheritance variadic-templates


    【解决方案1】:

    注意,基类是依赖于模板参数的,_fun是一个不依赖的名字,不会在依赖的基类中查找。

    您可以使名称 _fun 依赖,然后在实例化时查找它;那时确切的基础专业化是已知的。

    例如

    std::cout << std::get<0>(this->_fun);
    //                       ~~~~~~
    std::cout << std::get<0>(Fun<S<Ts...>>::_fun);
    //                       ~~~~~~~~~~~~~~~
    

    【讨论】:

    • 是的。在没有积极编码的情况下,我忘记了很多细节。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多