Function Templates gcc error shadow parameter

Hey guys,

i am trying to work with function parameters.

However once i pass 1 argument
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
                template<class F>
		void bar(int test, int test2)
		{
			F(test, test2);
		}

		template<class F>
		void bar()
		{
			F();
		}

		template<class F>
		void bar(int test)
		{
			F(test);  //< this line causes a shadow parameter error
		}


bar(int, int) and bar() compiles well. However bar(int) gives me a shadow parameter error.

declaration of 'F test' shadows a parameter


Does anybody know why?
F is a type, not an object (or function).
So F(test) is being interpretted as F test, i.e., declaring an F called test, which shadows the "int test" parameter.
Through the rules of syntax in C++, is parsing line 16 as a declaration of a variable of type F.

i.e. the same as doing F test;
This name of this variable, test, shadows the variable in your parameters, also called test.

tpb beat me:)
Last edited on
oh thx for the explanation. Now the error makes perfect sense.
But how can i tell C++ to interpret F(test) as Function F with the single argument test?
As tpb said, F isn't a function, it's the name of an arbitrary class (type). If you want to create an object of type F (which calls the 1-arg constructor), do something like
F obj(test);
or
F {test}; // unnamed, temporary object
Last edited on
Topic archived. No new replies allowed.