模板化类中模板化函数的困难

Jas*_*son 3 c++ templates c++11

在尝试编写递归模板成员函数以迭代元组时,我遇到了一个问题.

在以下代码中:

#include <cstddef>
#include <iostream>
#include <string>
#include <tuple>

template <typename... P>
class A
{
public:
  typedef std::tuple<P...> tup_t;

  tup_t tup;
};

template <typename T, typename... P>
class AA : public A<P...>
{
public:
  T junk;
};

template <typename T>
class B
{
public:
  T a;

  void func(const char* delim);

private:
  template <size_t x>
  void __func(const char* delim);
};

template <typename T>
void B<T>::func(const char* delim)
{
  __func<std::tuple_size<typename T::tup_t>::value>(delim);
}

template <typename T>
template <size_t x>
typename std::enable_if<(x > 1), void>::type
B<T>::__func(const char* delim)
{
  std::cout << std::get<x-1>(a.tup) << delim;

  __func<x-1>(delim);
}

template <typename T>
template <size_t x>
typename std::enable_if<(x == 1), void>::type
B<T>::__func(const char* delim)
{
  std::cout << std::get<x-1>(a.tup) << std::endl;
}

int main()
{
  typedef A<int,float,std::string> T_first;
  B<T_first> b;

  std::get<0>(b.a.tup) = 5;
  std::get<1>(b.a.tup) = 4.0;
  std::get<2>(b.a.tup) = "string";

  b.func(" - ");

  typedef AA<int,std::string,double,size_t> T_second;
  B<T_second> bb;

  std::get<0>(bb.a.tup) = "test";
  std::get<1>(bb.a.tup) = 3.0;
  std::get<2>(bb.a.tup) = std::tuple_size<T_second::tup_t>::value;

  bb.func(" => ");

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我编译时:

$ g++-4.5 -std=c++0x -W -Wall -pedantic-errors test6.cpp
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

test6.cpp:60:1: error: prototype for ‘typename std::enable_if<(x > 1), void>::type B<T>::__func(const char*)’ does not match any in class ‘B<T>’
test6.cpp:31:32: error: candidate is: template<class T> template<unsigned int x> void B::__func(const char*)
test6.cpp:70:1: error: prototype for ‘typename std::enable_if<(x == 1), void>::type B<T>::__func(const char*)’ does not match any in class ‘B<T>’
test6.cpp:31:32: error: candidate is: template<class T> template<unsigned int x> void B::__func(const char*)
Run Code Online (Sandbox Code Playgroud)

现在,如果我B<T>::__func在类中定义如下:

  template <size_t x>
  typename std::enable_if<(x > 1), void>::type
  __func(const char* delim)
  {
    std::cout << std::get<x-1>(a.tup) << delim;

    __func<x-1>(delim);
  }

  template <size_t x>
  typename std::enable_if<(x == 1), void>::type
  __func(const char* delim)
  {
    std::cout << std::get<x-1>(a.tup) << delim;
  }
Run Code Online (Sandbox Code Playgroud)

它汇编很好.

我真的不喜欢在类声明中实现这些函数,所以如果有人能指出我原来的尝试出错了,我会很感激.

是吗:

template <typename T>
template <size x>
Run Code Online (Sandbox Code Playgroud)

它应该用不同的方式写吗?

编译器版本: gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)

谢谢,

PS请不要取笑我的简化测试用例.产生这种情况的项目比这个例子更令人印象深刻...但只是略微.

Luc*_*ton 6

类型必须在声明和定义中匹配__func.所以在类定义中你必须声明__func为:

template <size_t x>
typename std::enable_if<(x > 1)>::type
__func(const char* delim);
Run Code Online (Sandbox Code Playgroud)

(这相当于使用std::enable_if<(x > 1), void>默认的第二个模板参数void.)

这种限制的原因与模板特化有关.事实上,因为你正在使用std::enable_if你依赖这些专业而std::enable_if<true, T>不是专业化.另请注意__func<0>,在不具有返回类型的情况下不匹配void.它有'返回类型std::enable_if<false, void>::type,它不存在,因此无效(然后通过SFINAE删除).