简体   繁体   English

返回类型是模板的模板

[英]Return type is a template of templates

I have the following class:我有以下 class:

  class Inject{ 

       template<template <class> class Daemon>
       RETURNTYPE register_inj();
  }

There are a number of implementation to this template function where the template arguments are explicitly specified one example is returning a class ServiceA which has one template argument that in this case is chosen to be class CacheA .这个模板 function 有很多实现,其中模板 arguments 被明确指定,一个例子是返回一个 class ServiceA ,它有一个模板参数,在这种情况下被选择为 class CacheA The rule is that the template function will always have one template argument, but that template argument can have multiple template template arguments. Eg ServiceB<typename A, typename B>规则是模板 function 将始终有一个模板参数,但该模板参数可以有多个模板模板 arguments。例如ServiceB<typename A, typename B>

For serviceA the funtion template is implemented to:对于serviceA ,函数模板实现为:

   template<>
   auto register_inj<ServiceA<CacheA>> 
   { impl... }

My question is what should RETURNTYPE be set to in the header file?我的问题是 header 文件中的RETURNTYPE应该设置成什么?

I believe this question mixes two separate things.我相信这个问题混合了两个不同的东西。

  1. The number of template arguments of the function template argument Daemon has nothing to with the return type. function 模板参数Daemon的模板 arguments 的编号与返回类型无关。 In fact, your specialisation for ServiceA indicates that you are not even asking for a template template argument.事实上,您对ServiceA的专业化表明您甚至没有要求模板模板参数。 ServiceA<CacheA> is not a template - it is a normal type (which happens to be an instantiation of a template ). ServiceA<CacheA>不是模板 - 它是普通类型(恰好是 template 的实例化)。 Your function should therefore simply be declared as template <class Daemon> RETURNTYPE register_inj() .因此,您的 function 应该简单地声明为template <class Daemon> RETURNTYPE register_inj() It will then work with ServiceA<CacheA> as well as with ServiceB<CacheB,ResolverB> (both of them are just types).然后它将与ServiceA<CacheA>以及ServiceB<CacheB,ResolverB>一起工作(它们都只是类型)。 You only need a template template parameter if you are instantiating that template within the body of your function.如果您在 function 的主体中实例化该模板,则只需要一个模板模板参数。

  2. In the header, the function return type could simply be declared to auto .在 header 中,function 返回类型可以简单地声明为auto That effectively declares the function to have a deduced return type .这有效地声明 function 具有推导的返回类型 However, you won't be able to use that function until it is defined (every call-site needs to know the return type of that function).但是,在定义 function 之前,您将无法使用它(每个调用站点都需要知道该函数的返回类型)。 You will therefore have to inline the function definition into your header as-well.因此,您还必须将 function 定义内联到您的 header 中。 If you want to avoid that, you will have to explicitly write the return type using meta-programming techniques, eg as follows:如果你想避免这种情况,你将不得不使用元编程技术显式地编写返回类型,例如:

// you need at least a forward definition of your services here,
// as well as the return types

template <typename Cache>
class ServiceA<Cache>;

template <typename Cache, typename Resolver>
class ServiceB<Cache,Resolver>;

class ServiceARegisterResult;

template <typename Resolver>
class ServiceBRegisterResult<Resolver>;

namespace detail {

template <typename Service>
struct register_inj_return_type {};

template <typename Cache>
struct register_inj_return_type<ServiceA<Cache>> : {
 using type = ServiceARegisterResult;
};

template <typename Cache, typename Resolver>
struct register_inj_return_type<ServiceB<Cache,Resolver>> : {
 using type = ServiceBRegisterResult<Resolver>;
};

} 
 
class Inject{ 

       template<class Daemon>
       typename detail::register_inj_return_type<Daemon>::type
       register_inj();
};

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

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