C++ template C2039 - c++

This compiles:
template <class T>
class Bar {};
template<class T, class Dummy=void>
class Foo;
template<class T>
class Foo <T, typename std::enable_if<
std::is_base_of<Bar<T>, T>::value
>::type> {
public:
//THIS CHANGES IN THE 2ND SNIPPET
void test () const {
std::cout << "test";
}
};
class Cat : Bar<Cat> {};
int main () {
Foo<Cat> foo;
foo.test();
return 0;
}
This errors:
template <class T>
class Bar {};
template<class T, class Dummy=void>
class Foo;
template<class T>
class Foo <T, typename std::enable_if<
std::is_base_of<Bar<T>, T>::value
>::type> {
public:
//THIS CHANGED!
void test () const;
};
//THIS WAS ADDED SINCE THE 1ST SNIPPET!
template<class T>
void Foo<T>::test () const {
std::cout << "test";
} //error C2039: 'test' : is not a member of 'Foo<T>'
class Cat : Bar<Cat> {};
int main () {
Foo<Cat> foo;
foo.test();
return 0;
}
I have marked the differences. Why does it error in the 2nd code snippet? How do I keep declaration and definition separate while avoiding the error?
I'm guessing it's got something to do with this:
"template
void Foo::test () const"
Like, this is the wrong way to tell the compiler that the method test() const is a method of template class template class Foo , T>::value >::type>
I've, of course, looked it up on Google and StackOverflow but it seems that, whenever this error pops up for templates, it's a different reason every time. (Probably because a lot of things can cause the C2039 error.)
Also, could a mod. or someone help me add the C2039 tag to this post? It says I need a min. of 1500 rep. to add that tag.
-- Rambling --
It also be noted that it's been a while since I've used C++; and even longer since I've used templates. I know this might be a weird way to use templates but I can assure you I have a valid reason!

An example of out-of-class member declaration for a partially specialized class template is given in 14.5.4.3/1 (C++03). And this is what it looks like
// primary template
template<class T, int I> struct A {
void f();
};
// class template partial specialization
template<class T> struct A<T,2> {
void g();
};
// member of class template partial specialization
template<class T> void A<T,2>::g() { }
As you can see, you have to specify specialized arguments in the out-of-class member definition.
In your case it should be
template<class T>
void Foo<T, typename std::enable_if<
std::is_base_of<Bar<T>, T>::value>::type>::test () const {
std::cout << "test";
}

Related

c++ template specialization with open template arguments [duplicate]

The following code:
template <typename S, typename T>
struct foo {
void bar();
};
template <typename T>
void foo <int, T>::bar() {
}
gives me the error
invalid use of incomplete type 'struct foo<int, T>'
declaration of 'struct foo<int, T>'
(I'm using gcc.) Is my syntax for partial specialization wrong? Note that if I remove the second argument:
template <typename S>
struct foo {
void bar();
};
template <>
void foo <int>::bar() {
}
then it compiles correctly.
You can't partially specialize a function. If you wish to do so on a member function, you must partially specialize the entire template (yes, it's irritating). On a large templated class, to partially specialize a function, you would need a workaround. Perhaps a templated member struct (e.g. template <typename U = T> struct Nested) would work. Or else you can try deriving from another template that partially specializes (works if you use the this->member notation, otherwise you will encounter compiler errors).
Although coppro mentioned two solutions already and Anonymous explained the second one, it took me quite some time to understand the first one. Maybe the following code is helpful for someone stumbling across this site, which still ranks high in google, like me. The example (passing a vector/array/single element of numericalT as dataT and then accessing it via [] or directly) is of course somewhat contrived, but should illustrate how you actually can come very close to partially specializing a member function by wrapping it in a partially specialized class.
/* The following circumvents the impossible partial specialization of
a member function
actualClass<dataT,numericalT,1>::access
as well as the non-nonsensical full specialisation of the possibly
very big actualClass. */
//helper:
template <typename dataT, typename numericalT, unsigned int dataDim>
class specialised{
public:
numericalT& access(dataT& x, const unsigned int index){return x[index];}
};
//partial specialisation:
template <typename dataT, typename numericalT>
class specialised<dataT,numericalT,1>{
public:
numericalT& access(dataT& x, const unsigned int index){return x;}
};
//your actual class:
template <typename dataT, typename numericalT, unsigned int dataDim>
class actualClass{
private:
dataT x;
specialised<dataT,numericalT,dataDim> accessor;
public:
//... for(int i=0;i<dataDim;++i) ...accessor.access(x,i) ...
};
If you need to partially specialise a constructor, you might try something like:
template <class T, int N>
struct thingBase
{
//Data members and other stuff.
};
template <class T, int N> struct thing : thingBase<T, N> {};
template <class T> struct thing<T, 42> : thingBase<T, 42>
{
thing(T * param1, wchar_t * param2)
{
//Special construction if N equals 42.
}
};
Note: this was anonymised from something I'm working on. You can also use this when you have a template class with lots and lots of members and you just want to add a function.
If you're reading this question then you might like to be reminded that although you can't partially specialise methods you can add a non-templated overload, which will be called in preference to the templated function. i.e.
struct A
{
template<typename T>
bool foo(T arg) { return true; }
bool foo(int arg) { return false; }
void bar()
{
bool test = foo(7); // Returns false
}
};
In C++ 17, I use "if constexpr" to avoid specialize (and rewrite) my method. For example :
template <size_t TSize>
struct A
{
void recursiveMethod();
};
template <size_t TSize>
void A<TSize>::recursiveMethod()
{
if constexpr (TSize == 1)
{
//[...] imple without subA
}
else
{
A<TSize - 1> subA;
//[...] imple
}
}
That avoid to specialize A<1>::recursiveMethod().
You can also use this method for type like this example :
template <typename T>
struct A
{
void foo();
};
template <typename T>
void A<T>::foo()
{
if constexpr (std::is_arithmetic_v<T>)
{
std::cout << "arithmetic" << std::endl;
}
else
{
std::cout << "other" << std::endl;
}
}
int main()
{
A<char*> a;
a.foo();
A<int> b;
b.foo();
}
output :
other
arithmetic

Class function template definition outside of the class

So i've been coding my own unique_ptr class and i have to handle arrays in a different way that i handle other types.
template <typename TYPE, bool _arr = std::is_array<TYPE>::value>
class scoped_ptr final {
private:
typedef std::remove_extent_t<TYPE> gen;
gen* m_data;
public:
//some methods
void reset();
};
template<typename TYPE ,bool _arr>
inline void scoped_ptr<TYPE, _arr>::reset()
{
//some code...
}
The problem is that i want the reset method to only be avaiable to non array allocations, when using std::enable_if i get the error: "A default template argument cannot be specified on the declaration of a member of a class template outside of its class" despite the code still compiling
template<typename TYPE ,bool _arr>
class scoped_ptr final {
public:
template<typename = typename std::enable_if<!_arr>::type>
void reset();
};
template<typename TYPE ,bool _arr>
template<typename = typename std::enable_if<!_arr>::type>
inline void scoped_ptr<TYPE, _arr>::reset()
{
}
I also tryed this, but it also gives an error: "template argument list must match parameter list"
template<typename TYPE ,bool _arr>
inline void scoped_ptr<TYPE, false>::reset()
{
}
Does anybody have an idea on how can i disable this method for arrays? I know i could specialize the class scoped_ptr but i wanted to avoid code duplication. Is there any way to do it?
Edit:
After reading the responses i changed the code to this:
template <typename TYPE, bool is_arr = std::is_array<TYPE>::value>
class scoped_ptr final {
private:
typedef std::remove_extent_t<TYPE> gen;
gen* m_data;
public:
//Some methods
template<bool _arr = is_arr, typename = typename std::enable_if<!_arr>::type>
void reset();
};
template<typename TYPE, bool is_arr>
template<bool, typename>
inline void scoped_ptr<TYPE, is_arr>::reset()
{
}
The code compiles with no errors, until i try to call the method:
int main() {
scoped_ptr<int> ptr = new int;
ptr.reset();
}
Thats when i get the error: "void scoped_ptr«int,false»::reset(void): could not deduce template argument for «unnamed-symbol»"
But if i write the implementation inside of the class the error goes away. How can i fix this?
If you want to make reset() SFINAE-friendly, make it a fake template:
template<bool is_arr = _arr, typename = std::enable_if_t<is_arr>>
void reset();
Note that SFINAE works when a template is instantiated, and the condition should depend on the template parameter. This is not a valid SFINAE construct:
template<typename = typename std::enable_if<!_arr>::type>
void reset();
If you don't care about SFINAE-friendliness, use static_assert() inside reset().
Edit.
Consider the following simple code as a demonstration of valid and invalid SFINAE:
template<class T, bool f = std::is_integral_v<T>>
struct S {
// template<typename = std::enable_if_t<f>> // (invalid)
template<bool _f = f, typename = std::enable_if_t<_f>> // (valid)
void reset() {}
};
template<class T, typename = void>
struct has_reset : std::false_type {};
template<class T>
struct has_reset<T, std::void_t<decltype(std::declval<T>().reset())>> : std::true_type {};
void foo() {
has_reset<S<int>>::value;
has_reset<S<void>>::value;
}
It will fail to compile if your replace the (valid) line with the (invalid) one.
Edit 2.
When you define a member function outside the class, you don't repeat default values of template parameters:
template<class T, bool f>
template<bool, typename>
void S<T, f>::reset() { }
Edit 3.
For some reason (a compiler bug I suppose) MSVC rejects this definition with the error: "Could not deduce template argument for «unnamed-symbol»". It can be fixed by adding a name for the bool parameter:
template<class T, bool f>
template<bool _f, typename>
void S<T, f>::reset() { }
This name should match that in the declaration.

overload of template template function - explicit call

So, first consider the following where template parameters are known implicitly from the function arguments:
#include <iostream>
using namespace std;
class A {};
class B {};
template <class T1, class T2>
class C {
T1 a;
T2 b;
};
template <class T1>
class D {
T1 a;
};
template <template<class, class> class TC, class TA, class TB>
void foo(TC<TA, TB> c) {
std::cout << "T<T,T>" << std::endl;
};
template <template<class> class TD, class TA>
void foo(TD<TA> d){
std::cout << "T<T>" << std::endl;
};
int main() {
C<A,B> c;
D<A> d;
foo(c);
foo(d);
}
And output is as you'd expect:
T<T,T>
T<T>
However, what if I don't have an instance of class C and D so I need to explicitly call the correct overload? How would this be done? i.e., have a main() that consists of:
int main() {
foo<C<A,B> >();
foo<D<A> >();
}
I've experimented with a few overloads of foo() as shown below:
template <template<class, class> class TC>
void foo() {
std::cout << "T<T,T>" << std::endl;
};
template <template<class> class TD>
void foo(){
std::cout << "T<T>" << std::endl;
};
template <template<class, class> class TC, class TA, class TB>
void foo() {
std::cout << "T<T,T>" << std::endl;
};
template <template<class> class TD, class TA>
void foo(){
std::cout << "T<T>" << std::endl;
};
However, this (and all permutations I've been able to think of) simply results in a series of errors along the lines of the (abbreviated) output shown below
prog.cpp: In function 'int main()':
prog.cpp:44:18: error: no matching function for call to 'foo()'
foo<C<A,B> >();
^
prog.cpp:44:18: note: candidates are:
prog.cpp:19:6: note: template<template<class, class> class TC> void foo()
void foo() {
^
prog.cpp:19:6: note: template argument deduction/substitution failed:
prog.cpp:24:6: note: template<template<class> class TD> void foo()
void foo(){
^
prog.cpp:24:6: note: template argument deduction/substitution failed:
Is what I'm looking to do even allowable? If so, where am I messing up?
---- EDIT ----
So as apple apple pointed out if my main() is as follows:
int main() {
foo<C, A, B>();
foo<D, A>();
}
I get the output as expected.
However, my real-world case winds up being more complex. I'll expand a bit here. The legacy code has (hundreds) of typedefs defined in headers elsewhere along the lines of:
typedef C<A, B> type_117;
typedef D<A> type_252;
The class I'm working on is templated and is instantiated with one of those typedefs as the templating argument. So something along the lines of:
template <class Type>
class Test
{
public:
Test();
SomeClass mSC;
}
Test::Test()
: mSC(foo<Type>())
{
};
where Test was instantiated as
Test<type_117> aTest;
So I've been trying to figure out how to write foo() for this context. At the point I call foo() within my Test's initializer am I able to "decompose" it to produce the <C,A,B> form? Or have I hit a roadblock and need to rework some of the existing framework?
template<class T>struct tag_t{constexpr tag_t(){}};
template<class T>constexpr tag_t<T> tag{};
these are type tags. They can be passed to functions without an instance of the type.
Template functions will deduce on them.
template <template<class, class> class TC, class TA, class TB>
void foo(tag_t<TC<TA, TB>>) {
std::cout << "T<T,T>" << std::endl;
};
template <template<class> class TD, class TA>
void foo(tag_t<TD<TA>>){
std::cout << "T<T>" << std::endl;
};
at call site do foo(tag<type_117>) and bob, as they say, is your uncle.
In C++98 (ick):
template<class T>struct tag_t{};
foo(tag_t<type_117>());
You may use partial specialization (and variadic template):
template <class Type>
class Test;
template <template <typename ...> class C, typename ... Ts>
class Test<C<Ts...>>
{
public:
Test() : mSC(foo<C, Ts...>()) {}
SomeClass mSC;
};
Take in count that partial specialization is forbidden for functions; so is difficult to do what do you exactly asked.
The suggestion from apple apple (chenge the calling as foo<C, A, B>() is a good one but, if you want to maintain the original call (foo<C<A, B>>()) you can use the fact that the partial specialization is allowed for structs/classes and create a partial specialization for a functor; something like
template <typename>
struct bar;
template <template<typename, typename> class Tc, typename Ta, typename Tb>
struct bar<Tc<Ta,Tb>>
{
void operator() ()
{ std::cout << "bar<Tc<Ta, Tb>>()" << std::endl; }
};
template <template<typename> class Tc, typename Ta>
struct bar<Tc<Ta>>
{
void operator() ()
{ std::cout << "bar<Tc<Ta>>()" << std::endl; }
};
The problem (?) is that, calling it, you can't call as bar<C<A,B>>() od bar<D<A>>() but you have to add a couple of parentheses:
bar<C<A,B>>()();
bar<D<A>>()();
or
bar<C<A,B>>{}();
bar<D<A>>{}();
I suppose that the functor solution can solve also the problem of the Edit part of your question.
If the added couple of parentheses is a problem, you can (as suggested by Jarod42 (thanks!)) wrap the call in a template function, as follows
template <typename T>
void bar ()
{ bar<T>{}(); }
So you can call the bar<C<A, B>>() function and manage the call in the specialized bar<C<A, B>> struct.
Observe also the solution from Jarod42: depending on your requirements, you could develop only a version of the partial specialization of bar.
-- EDIT --
The OP ask
I'm not that familiar with partial specialization; could you expand a bit on how what I was trying was?
Specialization (partial and full) is a big, big topic.
Just some example, to give an idea.
Given a template class/struct
template <typename X, typename Y>
struct foo
{ };
you can partial specialize it as follows (by example)
template <typename X>
struct foo<X, X>
{ };
when the specialization maintain a template variable, or you can full specialize as follow (by example)
template <>
struct foo<int, long>
{ };
where all template argument are fixed.
Well: with function you can full specialize but not partial specialize.
So you can write a template function
template <typename X, template Y>
void foo ()
{ }
and full specialize it
template <>
void foo<int, long> ()
{ }
but you can't partial specialize it; so you can't write (is an error)
template <typename X>
void foo<X, X> ()
{ }

Using std::enable_if and variadic base classes

Here's a cut down example of what I'm trying to do:
#include <string>
#include <iostream>
#include <type_traits>
template <typename T>
class foo
{
public:
template <typename U>
typename std::enable_if<std::is_same<T, U>::value>::type
bar(const U& t)
{
std::cout << t << "\n";
}
};
template <typename... Args>
class baz
: public foo<Args>...
{
};
int main()
{
baz<double, std::string> b;
b.bar(1.0);
}
This gives me ambiguous function errors:
error: request for member 'bar' is ambiguous
b.bar(1.0);
note: candidates are: template<class U> typename std::enable_if<std::is_same<T, U>::value>::type foo<T>::bar(const U&) [with U = U; T = std::basic_string<char>]
note: template<class U> typename std::enable_if<std::is_same<T, U>::value>::type foo<T>::bar(const U&) [with U = U; T = double]
My questions are twofold:
Why is the inner template U not deduced? I'm supposing that it's due to ordering of template deduction and overload resolution, but can someone explain this?
Is there another way of going about what I'm trying to do?
I think the error message is misleading. The problem is actually name bar is available in multiple base classes and you've not used using directive to bring the names you want into the derived class scope.
Here is one working solution:
template <typename X, typename... Args>
class baz : public foo<X>, public baz<Args...>
{
public:
using foo<X>::bar; //bring the name from the first base
using baz<Args...>::bar; //bring the name from the second base
};
template <typename X>
class baz<X> : public foo<X> //specialization for one argument
{
//no using directive needed, as there is one base only!
};
Complete Demo
The problem has nothing to do with variadic templates, template argument deduction, or the like. It is that member functions of the same name from different base classes don't overload. Minimized example:
struct foo {
void f(int &);
};
struct bar {
void f(const int &);
};
struct foobar : foo, bar { };
int main(){
foobar fb;
int i;
fb.f(i); // clang complains: error: member 'f' found in multiple base classes of different types
}
Since in your code, foo<double> and foo<std::string> are distinct types, and lookup for bar finds a declaration in each, your code is ill-formed.
A possible fix is to write a baz::bar that explicitly dispatches to the appropriate foo::bar:
template <typename... Args>
class baz
: public foo<Args>...
{
public:
template <typename U>
void
bar(const U& t)
{
foo<U>::bar(t);
}
};
You can SFINAE baz::bar on U being one of the types in Args, if desired.
Another possible solution is to use the recursive implementation shown in Nawaz's answer.

"invalid use of incomplete type" error with partial template specialization

The following code:
template <typename S, typename T>
struct foo {
void bar();
};
template <typename T>
void foo <int, T>::bar() {
}
gives me the error
invalid use of incomplete type 'struct foo<int, T>'
declaration of 'struct foo<int, T>'
(I'm using gcc.) Is my syntax for partial specialization wrong? Note that if I remove the second argument:
template <typename S>
struct foo {
void bar();
};
template <>
void foo <int>::bar() {
}
then it compiles correctly.
You can't partially specialize a function. If you wish to do so on a member function, you must partially specialize the entire template (yes, it's irritating). On a large templated class, to partially specialize a function, you would need a workaround. Perhaps a templated member struct (e.g. template <typename U = T> struct Nested) would work. Or else you can try deriving from another template that partially specializes (works if you use the this->member notation, otherwise you will encounter compiler errors).
Although coppro mentioned two solutions already and Anonymous explained the second one, it took me quite some time to understand the first one. Maybe the following code is helpful for someone stumbling across this site, which still ranks high in google, like me. The example (passing a vector/array/single element of numericalT as dataT and then accessing it via [] or directly) is of course somewhat contrived, but should illustrate how you actually can come very close to partially specializing a member function by wrapping it in a partially specialized class.
/* The following circumvents the impossible partial specialization of
a member function
actualClass<dataT,numericalT,1>::access
as well as the non-nonsensical full specialisation of the possibly
very big actualClass. */
//helper:
template <typename dataT, typename numericalT, unsigned int dataDim>
class specialised{
public:
numericalT& access(dataT& x, const unsigned int index){return x[index];}
};
//partial specialisation:
template <typename dataT, typename numericalT>
class specialised<dataT,numericalT,1>{
public:
numericalT& access(dataT& x, const unsigned int index){return x;}
};
//your actual class:
template <typename dataT, typename numericalT, unsigned int dataDim>
class actualClass{
private:
dataT x;
specialised<dataT,numericalT,dataDim> accessor;
public:
//... for(int i=0;i<dataDim;++i) ...accessor.access(x,i) ...
};
If you need to partially specialise a constructor, you might try something like:
template <class T, int N>
struct thingBase
{
//Data members and other stuff.
};
template <class T, int N> struct thing : thingBase<T, N> {};
template <class T> struct thing<T, 42> : thingBase<T, 42>
{
thing(T * param1, wchar_t * param2)
{
//Special construction if N equals 42.
}
};
Note: this was anonymised from something I'm working on. You can also use this when you have a template class with lots and lots of members and you just want to add a function.
If you're reading this question then you might like to be reminded that although you can't partially specialise methods you can add a non-templated overload, which will be called in preference to the templated function. i.e.
struct A
{
template<typename T>
bool foo(T arg) { return true; }
bool foo(int arg) { return false; }
void bar()
{
bool test = foo(7); // Returns false
}
};
In C++ 17, I use "if constexpr" to avoid specialize (and rewrite) my method. For example :
template <size_t TSize>
struct A
{
void recursiveMethod();
};
template <size_t TSize>
void A<TSize>::recursiveMethod()
{
if constexpr (TSize == 1)
{
//[...] imple without subA
}
else
{
A<TSize - 1> subA;
//[...] imple
}
}
That avoid to specialize A<1>::recursiveMethod().
You can also use this method for type like this example :
template <typename T>
struct A
{
void foo();
};
template <typename T>
void A<T>::foo()
{
if constexpr (std::is_arithmetic_v<T>)
{
std::cout << "arithmetic" << std::endl;
}
else
{
std::cout << "other" << std::endl;
}
}
int main()
{
A<char*> a;
a.foo();
A<int> b;
b.foo();
}
output :
other
arithmetic