c++ - 使用模板模板参数专门化基类

标签 c++ templates inheritance variadic-templates

我用我的脑袋敲了敲下面的代码:

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

我不明白这里发生了什么,也不知道为什么会出现该错误。一定有明显的东西我没看到...

最佳答案

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

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

例如

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

关于c++ - 使用模板模板参数专门化基类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50389805/

相关文章:

c++ - 我的 “stoi”函数或编译器有问题吗?

javascript - 如何使 Resig 的微模板与 XHTML 兼容?

c++ - 模板函数的前向声明

c++ - std::function 和 std::bind 行为

c++ - 在 C# 的 WPF 应用程序中单击按钮时如何调用 C++ 代码?

python - SWIG:返回原始指针与共享 ptrs 的 vector

c++ - C++ 中的模板 vector

java - 无法理解泛型和继承

c++ - 结构继承 : implicitly public or private?

Java - protected 访问修饰符