Difficulties with templated functions in templated classes
I'm running into an issue while trying to write a recursive template member function for iterating through tuples.
In the following code:
#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::g开发者_Go百科et<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;
}
When I compile with:
$ g++-4.5 -std=c++0x -W -Wall -pedantic-errors test6.cpp
I get the following errors:
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*)
Now, if I instead define B<T>::__func
inside the class like:
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;
}
It compiles fine.
I really don't like implementing the functions within the class declaration, so I'd appreciate it if someone could point out where my original attempt went wrong.
Is it:
template <typename T>
template <size x>
Should it be written in a different manner?
Compiler Version: gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)
Thanks,
P.S. Please don't make fun of my simplified test case. The project that spawned this scenario is a little more impressive than this example... but only slightly.
The types have to match in both the declaration and definition of __func
. So in the class definition you must declare __func
as:
template <size_t x>
typename std::enable_if<(x > 1)>::type
__func(const char* delim);
(This is equivalent to using std::enable_if<(x > 1), void>
as the second template parameter defaults to void
.)
The reason for this restriction has to do with template specializations. And in fact since you're using std::enable_if
you're relying on those specializations as std::enable_if<true, T>
is a specialization. Note also the mismatch in the case of __func<0>
which doesn't have return type void
. It 'has' return type std::enable_if<false, void>::type
, which doesn't exist, and hence is invalid (and is then removed via SFINAE).
精彩评论