initializer_list

Hi,

I'm having a syntax issue with initializer_lists, I just want to know if this is an issue with my compiler, my knowledge of c++, or whether it is not supported by the standard. I'm trying this with g++4.4.5 on Debian Squeeze.

The problem is that the commented line won't compile, it bails with

No match for `operator<' in `var < 5'

Thanks.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <initializer_list>
#include <iostream>

class X
{
public:
	template<int p>
	void calculate(const std::initializer_list<int> &il) {
		std::cout << "p: " << p << std::endl;
		std::cout << "n: " << il.size() << std::endl;

		int sum = 0;
		for(auto i = il.begin();i!=il.end();++i) {
			sum += *i;
		}

		std::cout << "sum: " << sum << std::endl;
	}
	template<int p>
	void operator()(const std::initializer_list<int> &il) {
		std::cout << "p: " << p << std::endl;
		std::cout << "n: " << il.size() << std::endl;

		int sum = 0;
		for(auto i = il.begin();i!=il.end();++i) {
			sum += *i;
		}

		std::cout << "sum: " << sum << std::endl;
	}

};

int main() {
	X var;

	for(int x=0;x!=3;++x) {
		for(int y=0;y!=3;++y) {
			var.calculate<5>({x,y});
			var.operator()<5>({x,y});
//			var<5>({x,y});
		}
	}

	return 0;
}
I don't think you it is allowed so you will probably have to write as you have done in the line above.

Can't you pass p as a normal parameter?
You can only specify template arguments after the name of a template. They make no sense after the name of a concrete object.

X::calculate() is a function template, so var.calculate<5>() is a template instantiation

X::operator() is a function template, so var.operator()<5>() is a template instantiation

var is an object, so var<5> is var less then 5 greater than ...

Moreover, X itself is not a template, so X<5> is not possible either. There simply isn't a way to provide template arguments to the function call operator here without writing it out explicitly, X().operator()<5>

Thanks for confirming, that, I assumed what you've both said was the case, I will try to rethink my design.
Topic archived. No new replies allowed.