1

编辑:让我们假设我有两个(或更多)模板函数f,并且g根据其模板参数使用(有时)类型:

template<typename T>
some_ugly_and_large_or_deep_template_struct_1<T>::type 
f(const some_ugly_and_large_or_deep_template_struct_1<T>::type&,
  const some_ugly_and_large_or_deeptemplate_struct_1<T>::type&)
{
   // body, that uses perhaps more times my
   // "some_ugly_and_large_or_deep_template_struct_1<T>"
}

template<typename T>
some_ugly_and_large_or_deep_template_struct_2<T>::type 
g(const some_ugly_and_large_or_deep_template_struct_2<T>::type&,
  const some_ugly_and_large_or_deeptemplate_struct_2<T>::type&)
{
   // body, that uses perhaps more times my
   // "some_ugly_and_large_or_deep_template_struct_2<T>"
}

我怎样才能简化这个“类型”定义?例如使用任何新的 C++11 工具?我只认为是这样的:

template<typename T,
         typename aux = some_ugly_and_large_or_deep_template_struct_1<T>::type>
aux f(const aux&, const aux&)
{
  // body, that uses perhaps more times my
  // "aux" type
}

template<typename T,
         typename aux = some_ugly_and_large_or_deep_template_struct_2<T>::type>
aux g(const aux&, const aux&)
{
  // body, that uses perhaps more times my
  // "aux" type
}

我用这种方法看到的问题是用户可以提供他自己的aux类型,而不是我想要的类型。

4

4 回答 4

5

如果将其设为可变参数模板,则调用者无法定义以下列出的类型参数:

template<typename T,
         typename..., // firewall, absorbs all supplied arguments
         typename aux = some_ugly_and_large_or_deep_template_struct_1<T>::type>
aux f(const aux&, const aux&)
{
  // body, that uses perhaps more times my
  // "aux" type
}

可选地,为了防止f使用太多模板参数意外调用,可以添加一个 static_assert:

template<typename T,
         typename... F,
         typename aux = some_ugly_and_large_or_deep_template_struct_1<T>::type>
aux f(const aux&, const aux&)
{
  static_assert(sizeof...(F)==0, "Too many template arguments");
  // body, that uses perhaps more times my
  // "aux" type
}

通常,我可以忍受让用户定义类型aux,例如,作为返回类型,这可以为您节省演员。

或者您可以将 替换static_assertenable_if

template<typename T,
         typename... F, typename = typename std::enable_if<sizeof...(F)==0>::type,
         typename aux = some_ugly_and_large_or_deep_template_struct<T>::type,>
aux f(const aux&, const aux&)
{
  // body, that uses perhaps more times my                                               
  // "aux" type                                                                          
}
于 2013-02-02T00:15:07.253 回答
2

您可以在函数旁边声明模板别名:

template<typename T> using f_parameter
  = typename some_ugly_and_large_or_deep_template_struct<T>::type;

template<typename T>
f_parameter<T> f(const f_parameter<T>&, const f_parameter<T>&)
{
   f_parameter<T> param;
}
于 2013-02-01T14:59:04.577 回答
1

一种可能的解决方案是将模板函数转换为template struct带有operator(). 例如:

#include <iostream>
#include <string>

template <typename T>
struct some_ugly_and_large_or_deep_template_struct
{
    typedef T type;
};

template <typename T>
struct f
{
    typedef typename some_ugly_and_large_or_deep_template_struct<T>::type aux;

    aux operator()(const aux& a1, const aux& a2)
    {
        return a1 + a2;
    }
};

int main()
{
    std::cout << f<int>()(4, 4) << "\n";
    std::cout << f<std::string>()("hello ", "world") << "\n";
    return 0;
}
于 2013-02-01T14:52:30.390 回答
1

你可以使用类似的东西

namespace f_aux {
   template <typename T> using type = 
            typename some_ugly_and_large_or_deep_template_struct<T>::type;
}

template <typename T>
f_aux::type<T> f(const f_aux::type<T>& , const f_aux::type<T>&);

如果 f 的声明位于合适的命名空间或类中,则可能不需要额外的f_aux命名空间。

于 2013-02-01T15:02:56.593 回答