Today, I came across two error messages which I never seen before. It's completely new to me.
Here is the code:
template<typename T>
struct adder { adder(const T &item) { } };
template<typename T>
void initializer(const T &item) { adder<T>(item); }
int main()
{
initializer("const string literal");
}
On compiling, GCC gives these errors:
prog.cpp: In function ‘void initializer(const T&)’:
prog.cpp:6: error: declaration of ‘adder<T> item’ shadows a parameter
prog.cpp: In function ‘void initializer(const T&) [with T = char [21]]’:
prog.cpp:10: instantiated from here
prog.cpp:6: error: declaration of ‘adder<char [21]> item’ shadows a parameter
prog.cpp:6: error: no matching function for call to ‘adder<char [21]>::adder()’
prog.cpp:3: note: candidates are: adder<T>::adder(const T&) [with T = char [21]]
prog.cpp:3: note: adder<char [21]>::adder(const adder<char [21]>&)
See the bold text. One error is shown twice, which is this
error: declaration of ‘adder<T> item’ shadows a parameter
error: declaration of ‘adder<char [21]> item’ shadows a parameter
What does it mean? Why does it show twice with different template arguments? First one with T
, second one with char [21]
?
EDIT: does adder<T>(item)
declare variable with name item? But that is not what I intended. I think it should create a temporary object passing item as argument to the constructor.
I would like to know the section from the Standard which deals with this issue!
Other interesting error is this:
error: no matching function for call to ‘adder<char [21]>::adder()’
Which indicates that the compiler is looking for default constructor? But I'm wondering why is the compiler looking for it when in fact my code doesn't use it at line 6?
Code at ideone : http://www.ideone.com/jrdLL