template function parameter inside template class

Sorry for the big code block but it can be compiled.

I get this strange error. It seems that it doesn't recognize function template parameter.
In instantiation of 'DenseVectorArrayT<T, S>& DenseVectorArrayT<T, S>::operator-=(const DenseVectorT<C>&) [with C = std::array<int, 2ull>; T = int; long long unsigned int S = 2ull; DenseVectorArrayT<T, S> = DenseVectorArrayT<int, 2ull>]':
1.cpp:30:13:   required from here
1.cpp:24:60: error: invalid operands of types '<unresolved overloaded function type>' and 'bool' to binary 'operator<'


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <array>

template<typename C>
struct DenseVectorT : public C
{
protected:
	template<bool Add = true, typename C2 = C>
	static void add(C &v1, const C2 &v2)
	{
		for (typename C::size_type z = 0; z < v2.size(); z++)
			v1[z] += (Add ? 1 : -1) * v2[z];
	}
};

template<typename T, size_t S>
class DenseVectorArrayT : public DenseVectorT<std::array<T, S>>
{
	typedef DenseVectorT<std::array<T, S>> parent;

public:
	template<typename C>
	DenseVectorArrayT &operator+=(const DenseVectorT<C> &v) { parent::add(*this, v); return *this; }
	template<typename C>
	DenseVectorArrayT &operator-=(const DenseVectorT<C> &v) { parent::add<false>(*this, v); return *this; }   // <--- HERE IT IS!!!
};

int main()
{
	DenseVectorArrayT<int, 2> v[2];
	v[0] -= v[1];
	return 0;
}
Last edited on
You forgot to tell it that "add" is a template:

DenseVectorArrayT &operator-=(const DenseVectorT<C> &v) { parent::template add<false>(*this, v); return *this; } // <--- HERE IT IS!!!

(in case you wonder how the error message came about, the compiler saw the function name "add" (an unresolved overloaded function type"), operator less-then, and the boolean literal "false", and said it has no idea how to compile "add < false")
Last edited on
Nice answer Cubbi...I'm a bit confused as well though. Why would the compiler need to be told add is a template? It has already seen the symbol, and knows it is a template - how does this differ from any other template call?
I get the point now.
But, dude! What a strange syntax!
C++ syntax continues for 10th year to surprise me!

Thank you very much.
Topic archived. No new replies allowed.