Templates 可变模板类中定义的不同类型
我试图允许这些类的不同用户通过监视器向DoerT传递不同的配置。如果有人想使用“myU”,他们可以,如果没有,他们不会被迫通过。然后我想为这个特定的NYConfig专门化DoerT的一个成员函数。有几个“干线”使这成为可能。我收到编译器错误:例如:Templates 可变模板类中定义的不同类型,templates,overloading,variadic,Templates,Overloading,Variadic,我试图允许这些类的不同用户通过监视器向DoerT传递不同的配置。如果有人想使用“myU”,他们可以,如果没有,他们不会被迫通过。然后我想为这个特定的NYConfig专门化DoerT的一个成员函数。有几个“干线”使这成为可能。我收到编译器错误:例如: Config.H #include "Doer.H" template<class T> struct ConfigT { typedef T myT; typedef DoerT<ConfigT> D
Config.H
#include "Doer.H"
template<class T>
struct ConfigT
{
typedef T myT;
typedef DoerT<ConfigT> Doer;
};
template<class T, class U>
struct NYConfig
{
typedef T myT;
typedef U myU;
typedef DoerT<NYConfig> Doer;
};
Monitor.H
#include "Config.H"
#include "Doer.H"
template<typename ...monitors>
struct Monitor;
template<class T>
struct Monitor<T>
{
typedef ConfigT<T> Config;
typedef DoerT<Config> Doer;
};
template<class T, class U>
struct Monitor<T,U>
{
typedef NYConfig<T,U> Config;
typedef DoerT<Config> Doer;
};
Doer.H
#include "Config.H"
template<class ConfigT>
class DoerT
{
bool start();
};
template<class ConfigT>
bool DoerT<ConfigT>::start() {....}
//error is here:
//error 'NYConfig' was not declared in this scope.
//I've also tried <NYConfig<myT,myU> >::start
template<>
bool DoerT<NYConfig>::start()
{
if(NYConfig::u == "yes")
return true;
return false;
}
Main1.C
struct myT {int t; };
struct myU {std::string u};
typedef Monitor<myT,myU> monitor;
typedef monitor::Config Config;
static monitor m;
Main2.C
struct myT2 {int t2; }
typedef Monitor<myT2> monitor;
typedef monitor::Config Config;
static monitor m;
Config.H
#包括“Doer.H”
模板
结构配置
{
typedef T myT;
类型def DoerT Doer;
};
模板
结构NYConfig
{
typedef T myT;
类型def U myU;
类型def DoerT Doer;
};
监视器.H
#包括“Config.H”
#包括“Doer.H”
模板
结构监视器;
模板
结构监视器
{
typedef-ConfigT-Config;
类型def DoerT Doer;
};
模板
结构监视器
{
typedef-NYConfig;
类型def DoerT Doer;
};
多尔
#包括“Config.H”
模板
类多特
{
bool start();
};
模板
bool DoerT::start(){….}
//错误如下:
//在此作用域中未声明错误“NYConfig”。
//我也试过::开始
模板
bool DoerT::start()
{
如果(NYConfig::u==“是”)
返回true;
返回false;
}
main 1.C
结构myT{int t;};
结构myU{std::string u};
typedef监视器;
类型定义监视器::配置;
静态监视器m;
main 2.C
结构myT2{int t2;}
typedef监视器;
类型定义监视器::配置;
静态监视器m;
我可能遗漏了一些东西(我正在扫描,因为我正要喝茶!),但我认为DoerT永远不会起作用,因为NYConfig本身就是一个模板,所以它不能显得不专业,除非它是一个模板参数,而这不是因为您专门使用该错误函数。另外,myT和myU不是在NYConfig(和ConfigT)中定义的吗?因为如果它们是,您就不可能将它们作为模板参数传递给NYConfig.EDIT:我现在看到您在文件的末尾定义了myU和myT,我忽略了这一点,但它们可以;不能在标题中使用。我正在研究这个问题,有几个问题,但您看到的错误是由于循环依赖性问题造成的,您在Config.H中包含Doer.H,反之亦然。是否希望Monitor在传递两个参数时始终使用NYConfig,在传递一个参数时始终使用ConfigT?或者,传递多少参数来监控是否使用ConfigT或NYConfig是一个独立的问题?我可能遗漏了一些东西(我正在扫描,因为我正要喝茶!),但我认为DoerT永远不会起作用,因为NYConfig本身就是一个模板,所以它不能显得不专业,除非它是一个模板参数,这并不是因为你在用错误来专门处理这个函数。另外,myT和myU不是在NYConfig(和ConfigT)中定义的吗?因为如果它们是,您就不可能将它们作为模板参数传递给NYConfig.EDIT:我现在看到您在文件的末尾定义了myU和myT,我忽略了这一点,但它们可以;不能在标题中使用。我正在研究这个问题,有几个问题,但您看到的错误是由于循环依赖性问题造成的,您在Config.H中包含Doer.H,反之亦然。是否希望Monitor在传递两个参数时始终使用NYConfig,在传递一个参数时始终使用ConfigT?或者,传递多少参数来监视与是否使用ConfigT或NYConfig无关的问题?