I have a code, which compiles successfully (g++ 4.9.2):
#include <iostream>
#include <utility>
// general function for any variadic templated argument
template<template<typename ...> class T, typename ...TTs>
void
foo(T<TTs...>& arg)
{
std::cout << "T<TTs...>" << std::endl;
}
template<typename ...Ts>
struct xxx
{
// not important
};
// specialization for only variadic templated xxx
template<typename ...TTs> void
foo(xxx<TTs...>& arg)
{
std::cout << "xxx<TTs...>" << std::endl;
}
// specialization for non-variadic templated xxx
template<typename TT> void
foo(xxx<TT>& arg)
{
std::cout << "xxx<TT>" << std::endl;
}
// specialization for xxx<uint8_t>
template<> void
foo(xxx<uint8_t>& arg)
{
std::cout << "xxx<uint8_t>" << std::endl;
}
int
main(int argc, char** argv)
{
xxx<uint8_t> x1;
std::cout << "xxx<uint8_t> => ";
foo(x1);
xxx<uint16_t> x2;
std::cout << "xxx<uint16_t> => ";
foo(x2);
xxx<uint8_t,uint16_t> x3;
std::cout << "xxx<uint8_t,uint16_t> => ";
foo(x3);
std::pair<uint8_t,uint16_t> x4;
std::cout << "std::pair<uint8_t,uint16_t> => ";
foo(x4);
return 0;
}
and produces:
xxx<uint8_t> => xxx<uint8_t>
xxx<uint16_t> => xxx<TT>
xxx<uint8_t,uint16_t> => xxx<TTs...>
std::pair<uint8_t,uint16_t> => T<TTs...>
Now I want to have these foo methods inside a class, and write:
#include <iostream>
#include <utility>
class abc
{
public:
// general function for any variadic templated argument
template<template<typename ...> class T, typename ...TTs>
void
foo(T<TTs...>& arg)
{
std::cout << "T<TTs...>" << std::endl;
}
};
template<typename ...Ts>
struct xxx
{
// not important
};
// specialization for only variadic templated xxx
template<typename ...TTs> void
abc::foo(xxx<TTs...>& arg)
{
std::cout << "xxx<TTs...>" << std::endl;
}
// specialization for non-variadic templated xxx
template<typename TT> void
abc::foo(xxx<TT>& arg)
{
std::cout << "xxx<TT>" << std::endl;
}
// specialization for xxx<uint8_t>
template<> void
abc::foo(xxx<uint8_t>& arg)
{
std::cout << "xxx<uint8_t>" << std::endl;
}
int
main(int argc, char** argv)
{
abc p;
xxx<uint8_t> x1;
std::cout << "xxx<uint8_t> => ";
p.foo(x1);
xxx<uint16_t> x2;
std::cout << "xxx<uint16_t> => ";
p.foo(x2);
xxx<uint8_t,uint16_t> x3;
std::cout << "xxx<uint8_t,uint16_t> => ";
p.foo(x3);
std::pair<uint8_t,uint16_t> x4;
std::cout << "std::pair<uint8_t,uint16_t> => ";
p.foo(x4);
return 0;
}
and this produces a compilation error:
test_ko.cc:24:1: error: prototype for ‘void abc::foo(xxx<TTs ...>&)’ does not match any in class ‘abc’
abc::foo(xxx<TTs...>& arg)
^
test_ko.cc:10:5: error: candidate is: template<template<class ...> class T, class ... TTs> void abc::foo(T<TTs ...>&)
foo(T<TTs...>& arg)
^
test_ko.cc:31:1: error: prototype for ‘void abc::foo(xxx<TT>&)’ does not match any in class ‘abc’
abc::foo(xxx<TT>& arg)
^
test_ko.cc:10:5: error: candidate is: template<template<class ...> class T, class ... TTs> void abc::foo(T<TTs ...>&)
foo(T<TTs...>& arg)
I want a specialization for foo methods, not declaring a new signature inside abc class (because there is a class with templates, a part of a library, and separate specialization, for later declared xxx class).
It looks for me that code with methods is similar to one with functions, but I get error here. What I do wrong?
// general function for any variadic templated argument
template<template<typename ...> class T, typename ...TTs>
void
foo(T<TTs...>& arg)
{
std::cout << "T<TTs...>" << std::endl;
}
This is a template function.
template<typename ...Ts>
struct xxx
{
// not important
};
// specialization for only variadic templated xxx
template<typename ...TTs> void
foo(xxx<TTs...>& arg)
{
std::cout << "xxx<TTs...>" << std::endl;
}
This is not a specialization. This is a different template function whose name foo overloads with the above template function.
// specialization for non-variadic templated xxx
template<typename TT> void
foo(xxx<TT>& arg)
{
std::cout << "xxx<TT>" << std::endl;
}
This is not a specialization. This is a different template function whose name foo overloads with the above template functions.
// specialization for xxx<uint8_t>
template<> void
foo(xxx<uint8_t>& arg)
{
std::cout << "xxx<uint8_t>" << std::endl;
}
This is a full specialization of one of the above template functions. I think the 3rd one, but I wouldn't bet money on it. (I believe it specializes whichever template xxx<uint8_t>& would be dispatched to if you called foo).
Instead of this full specialization, I would instead write:
inline foo(xxx<uint8_t>& arg)
{
std::cout << "xxx<uint8_t>" << std::endl;
}
which is a brand new overload of foo again. Overloads are far less quirky than full function specializations.
There is no such thing as partial template function specialization.
This explains why your attempt to specialize methods with the same syntax doesn't work. There is no such thing as partial template member function specialization either.
You either have to write the overloads in the class itself, or dispatch to a different context.
What "went wrong" and was the proximate cause of the error is that your initial code introduced new overloads. You are not permitted to introduce new overloads of a method outside of the class definition, so the compiler pointed out your error.
Here is a useful technique. We are in abc:
// general function for any variadic templated argument
template<template<typename ...> class T, typename ...TTs>
void foo(T<TTs...>& arg)
{
return foo(*this, arg);
}
private:
template<template<typename ...> class T, typename ...TTs>
friend void foo(abc& self, T<TTs...>& arg)
{
std::cout << "T<TTs...>" << std::endl;
}
Our foo method extends to a foo friend.
We then add code in the same namespace as abc:
template<typename ...TTs> void
foo(abc& self, xxx<TTs...>& arg)
{
std::cout << "xxx<TTs...>" << std::endl;
}
template<typename TT> void
foo(abc& self, xxx<TT>& arg)
{
std::cout << "xxx<TT>" << std::endl;
}
inline void foo(abc& self, xxx<uint8_t>& arg)
{
std::cout << "xxx<uint8_t>" << std::endl;
}
and they are found via ADL when abc::foo is called.
Related
I can't seem to wrap my head around what is going wrong with the following example
$ cat repro.cpp
#include <iostream>
using namespace std;
template<class T>
struct S1_t
{
template<class U>
struct inner_t_x {};
using inner_t = inner_t_x<T>;
};
struct Bogus{};
template<class T>
struct Functor1
{
void operator()()
{
cout << "Functor1 FAIL: " << __PRETTY_FUNCTION__ << endl;
}
};
template<>
struct Functor1<typename S1_t<Bogus>::template inner_t_x<Bogus>>
{
void operator()()
{
cout << "Functor1 PASS: " << __PRETTY_FUNCTION__ << endl;
}
};
template<class T>
struct Functor2
{
void operator()()
{
cout << "Functor2 FAIL: " << __PRETTY_FUNCTION__ << endl;
}
};
template<class T>
struct Functor2<typename S1_t<T>::template inner_t_x<T>>
{
void operator()()
{
cout << "Functor2 PASS: " << __PRETTY_FUNCTION__ << endl;
}
};
template<class T>
void eval()
{
Functor1<T>{}();
Functor2<T>{}();
}
int main()
{
eval<S1_t<Bogus>::inner_t>();
return 0;
}
$ clang++ repro.cpp -std=c++11 -Wall && ./a.out
Functor1 PASS: void Functor1<S1_t<Bogus>::inner_t_x<Bogus> >::operator()()
Functor2 FAIL: void Functor2<S1_t<Bogus>::inner_t_x<Bogus> >::operator()() [T = S1_t<Bogus>::inner_t_x<Bogus>]
In the Functor1, I explicitly specialise on the type Bogus, and indeed that specialisation is being invoked. However, in the Functor2 the type is allowed to be deduced, but partial specialisation fails and generic template is instantiated instead. Yet, __PRETTY_PRINT__ show the same signature for Functor1 & Functort2 when instantiated.
Can anyone able to explain this behaviour, and is there a way to fix Functor2 partial specialisation to fit this scenario? Thanks!
fwiw: Changing Functor2 partial specialisation to
template<class T>
struct Functor2<typename S1_t<Bogus>::template inner_t_x<T>>
{
void operator()()
{
cout << "Functor2 PASS: " << __PRETTY_FUNCTION__ << endl;
}
};
gives correct output
Functor1 PASS: void Functor1<S1_t<Bogus>::inner_t_x<Bogus> >::operator()()
Functor2 PASS: void Functor2<S1_t<Bogus>::inner_t_x<Bogus> >::operator()() [T = S1_t<Bogus>::inner_t_x<Bogus>]
but this is not the option in my use case.
typename S1_t<T>::..is not deducible in partial specialization.
You got a clearer error when using inner_t instead of inner_t_x<T>:
main.cpp:41:8: error: template parameters not deducible in partial specialization:
struct Functor2<typename S1_t<T>::inner_t>
^
main.cpp:41:8: note: 'T'
main.cpp:41:8: warning: class template partial specialization contains a template parameter that cannot be deduced; this partial specialization will never be used
The specialization of Functor2 will never be used. The compiler could issue a warning about that, but it doesn't in your case. The reason for it being not deducible is simple: Imagine you later add something like
struct Hogus;
template<>
struct S1_t<Hogus>
{
template <typename U>
using inner_t_x = S1_t<Bogus>::inner_t_x<Bogus>;
};
then S1_t<Bogus>::inner_t_x<Bogus> and S1_t<Hogus>::inner_t_x<Hogus> would be the same type. Therefore, the template deduction in the partial specialization of Functor2 could yield both T=Bogus or T=Hogus. Hence, it cannot be deduced unambiguously under any circumstances.
I have a templated class and I want to use a create a templated function inside that class. I can't seem to figure out now to do it.
I boiled it down to a simple program:
#include <iostream>
template<typename TInputType = short,
typename TInternalType = float>
class MyClass
{
public:
void Print();
template<typename TAnotherType> void DoSomething(TAnotherType t);
};
template<typename TInputType, typename TInternalType>
void MyClass<TInputType,TInternalType>::Print()
{
printf("whats up\n");
}
template<typename TInputType, typename TInternalType, typename TAnotherType>
void MyClass<TInputType,TInternalType>::DoSomething(TAnotherType t)
{
std::cout << "whats up:" << t << std::endl;
}
int main() {
MyClass<> tst;
tst.Print();
tst.DoSomething<int>(10);
std::cout << "!!!Hello World!!!" << std::endl;
return 0;
}
I get error: invalid use of incomplete type OR error: too many template parameters in template redeclaration
Ok so... Ive been experimenting and I figured it out.
You need two template calls
...
template<typename TInputType, typename TInternalType>
template<typename TAnotherType>
void MyClass<TInputType,TInternalType>::DoSomething(TAnotherType t)
{
std::cout << "whats up:" << t << std::endl;
}
...
I want to unpack the parameter pack in func (see line A), but it doesnt work. How can I unpack inside func< > or modify Line A only?
#include <iostream>
using namespace std;
void func()
{
cerr << "EMPTY" << endl;
}
template <class A, class ...B> void func()
{
cerr << "A: " << endl;
func<B... >(); // line A
}
int main(void)
{
func<int,int>();
return 0;
}
An expected output :
A:
A:
edited:
all of answers are very good. thanks alot
Sometimes it's easier to unpack everything at once, instead of recursively. If you simply want a parameter pack for_each, you can use a variant of the braced-init-list expansion trick (Live demo at Coliru):
template <class A>
void process_one_type() {
cerr << typeid(A).name() << ' ';
}
template <class ...B> void func()
{
int _[] = {0, (process_one_type<B>(), 0)...};
(void)_;
cerr << '\n';
}
By using func<B... >(); you are implying that func is a function template, but your previously defined func() is not.
You need to define a func() template that accepts zero template arguments. Here's a working example (on g++ 4.8.1):
#include <iostream>
using namespace std;
void func()
{
cerr << "EMPTY" << endl;
}
template <class ... B>
typename std::enable_if<sizeof...(B) == 0>::type func()
{
}
template <class A, class ...B> void func()
{
cerr << "A: " << endl;
func<B... >(); // line A
}
int main(void)
{
func(); // This outputs EMPTY
func<int,int>(); // This will not output EMPTY
return 0;
}
Try this:
template <class A> void func()
{
cerr << "A: " << endl;
}
template <class A, class B, class ...C> void func()
{
cerr << "A: " << endl;
func<B, C...>(); // line A
}
Consider what the invocation of the recursive call func<B...>(); looks like when B... is empty. It's calling func<>(); but the definition of your attempted base case func() is not a template function, ie. you can't call it via func<>();
Since we don't have partial specialization for function templates yet, (hopefully it will be supported soon) one way to do it is to use a class template to do the partial specialization and use the function to simply delegate the work to the class template.
#include <iostream>
/* Forward declaration. */
template <typename... T>
struct FuncImpl;
/* Base case. */
template <>
struct FuncImpl<> {
void operator()() const {
std::cout << "Base case" << std::endl;
}
}; // FuncImpl<>
/* Recursive case. */
template <typename First, typename... Rest>
struct FuncImpl<First, Rest...> {
void operator()() const {
std::cout << "Recursive case" << std::endl;
FuncImpl<Rest...>()();
}
}; // FuncImpl<First, Rest...>
/* Delegate function. */
template <typename... T>
void Func() {
FuncImpl<T...>()();
}
int main() {
Func<>();
Func<int, double>();
}
Personally I think this solution is cleaner than other solutions such as tagged dispatching or SFINAE, despite the cruft around operator()s.
Why is it that this compiles:
class Testable {
public:
template <bool flag>
typename std::enable_if<flag>::type
void foo() { cout << "Yay" << endl; }
template <bool flag>
typename std::enable_if<!flag>::type
void foo() { cout << "Nay" << endl; }
};
But not if I define both foos using default types like so:
template <bool flag, typename = typename std::enable_if<flag>::type>
void foo() { cout << "Yay" << endl; } // (A)
template <bool flag, typename = typename std::enable_if<!flag>::type>
void foo() { cout << "Nay" << endl; } // (B)
I get this error (first line pointing to the definition of (B), second one pointing to (A)):
error: 'template<bool flag, class> void Testable::foo()' cannot be overloaded
error: with 'template<bool flag, class>> void Testable::foo()'
The compiler complains because the two function templates have the same signature. Paragraph 1.3.18 of the C++11 Standard specifies what the signature of a function template is defined by:
<function template> name, parameter type list (8.3.5), enclosing namespace (if any), return type, and
template parameter list
As you can see, default template arguments are not part of the signature.
You could work around this problem by changing your definition of Testable as follows:
class Testable {
public:
template <bool flag, typename std::enable_if<flag>::type* = nullptr>
void foo() { cout << "Yay" << endl; } // (A)
template <bool flag, typename std::enable_if<!flag>::type* = nullptr>
void foo() { cout << "Nay" << endl; } // (B)
};
As noted, two functions cannot have the same signature.
However, I have a different fix.
template<std::size_t>
struct secret_enum { enum class type {}; };
template<bool b, std::size_t n=0>
using EnableIf = typename std::enable_if< b, typename secret_enum<n>::type >::type;
class Testable {
public:
template <bool flag, EnableIf<flag, 0>...>
void foo() { cout << "Yay" << endl; } // (A)
template <bool flag, EnableIf<!flag, 1>...>
void foo() { cout << "Nay" << endl; } // (B)
};
where you enumerate your overloads with 0, 1, etc, which generates a possible type, which you then ... to say "0 or more of these", and the types generated are basically impossible to generate enum instances.
Sadly, this doesn't work in clang 3.2. It does in gcc 4.8.
I would like to specialize a template method for a class C that is itself
templated by an int parameter.
How do I do this?
template <int D=1>
class C {
static std::string foo () { stringstream ss; ss << D << endl; return ss.str();}
};
template <class X>
void test() { cout << "This is a test" << endl;}
template <>
template <int D>
void test<C<D> > () {cout << C<D>::foo() << endl;}
The specialization for test() fails with "Too many template parameter lists in declaration of void test()".
Function template partial specialization is not allowed. Do
template <int D>
void test () {cout << C<D>::foo() << endl;}
You don't want the first template<> on your partial specialisation of test<C<D>>. Moreover, you can only partially specialise class templates, not function templates. Something like this might work:
template <class X>
struct thing
{
static void test() { cout << "This is a test" << endl;}
};
template <int D>
struct thing<C<D>>
{
static void test() {cout << C<D>::foo() << endl;}
};
If your function template took an argument, and used that to infer the template argument, then you could get a similar effect using overloading, something like:
template <class X>
void test(const X&) { cout << "This is a test" << endl;}
template <int D>
void test(const C<D>&) {cout << C<D>::foo() << endl;}
test(3); // calls first version
test(C<3>()); // calls second version