简体   繁体   English

在C ++中使用模板类的模板类

[英]Usage of template class with template method in C++

I have a class, which have a public templated methods. 我有一个类,它有一个公共的模板方法。 This class has 2 strategies of behavior, which i want to pass via class template. 这个类有两种行为策略,我想通过类模板传递。

template<class Strategy>
class SomeClass {
public:
    template<class B>
    void ProcessType(){}
};

// And do something like this:
SomeClass<Strategy1> sc();
sc.ProcessType<SomeClassType>();
sc.ProcessType<SomeClassType2>();

SomeClass<Strategy2> sc2();
sc2.ProcessType<SomeClassType>();
sc2.ProcessType<SomeClassType2>();

But this code doesn't compile. 但是这段代码没有编译。 I need to keep usage exact like this (to manipulate just via strategy). 我需要像这样保持用法(仅通过策略操纵)。

This is the problem: 这就是问题:

SomeClass<Strategy1> sc();

That is a declaration of a function called sc that takes no arguments and returns a SomeClass<Strategy1> . 这是一个名为sc的函数声明,它不带参数并返回SomeClass<Strategy1> This is commonly known as a vexing parse (but not the most vexing parse ). 这通常被称为烦恼的解析(但不是令人烦恼的解析 )。 What you want is: 你想要的是:

SomeClass<Strategy1> sc;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM