模板类的嵌套类的类的部分特化

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

我想为嵌套在模板类中的类部分特化一个类,就像这样

template <typename T, typename U = void>
struct base {
  void print() {
    std::cout << "base specialization" << std::endl;
  }
};

template <typename A, typename B>
struct Outer {
  struct Inner {
    A a;
    B b;
  };
};

// What I am trying to achieve
template< params... >
struct base < specialization ... > {
  void print() {
    std::cout << "Outer::Inner specialization" << std::endl;
  }
};

对于类型名称的所有 A、B 组合都是如此

我想实现

base<Outer<int, char>::Inner>().print();
base<Outer<char, char>::Inner>().print();
base<Outer<int, float>::Inner>().print();

打印

Outer::Inner specialization

c++ c++11 templates
1个回答
0
投票

所说的

int::char
您可能指的是
int, char
。 与您想要实现的目标接近的一件事是

template <typename A, typename B>
struct base <Outer<A, B>> {
  void print() {
    std::cout << "Outer::Inner specialization" << std::endl;
  }
};

int main() {
    base<Outer<int, char>>().print(); // prints "Outer::Inner specialization"
}

无法从

int
推出
char
Outer<int, char>::Inner
,因为
::Inner
是非推导上下文(请参阅什么是非推导上下文?,第一个标题),但您可以推导它们来自
Outer<int, char>

© www.soinside.com 2019 - 2024. All rights reserved.