17

I get the following compiler¹ message

main.cpp: In instantiation of ‘void fkt(Foo) [with Foo = int]’:
main.cpp:5:7:   required from here

The binary is created anyway, so it's not an error. But it's also not marked as a warning. What is this message and why do I get it?

I reduced the code to the following example

template <typename Foo>
void fkt(Foo f) {}

int main() {
  fkt(1);
  return 0;
}

¹ gcc 4.7.2

EDIT: Here the steps to reproduce:

% cat main.cpp
template <typename Foo>
void fkt(Foo f) {}

int main() {
  fkt(1);
  return 0;
}
% g++ -Wall  -Wextra main.cpp
main.cpp: In instantiation of ‘void fkt(Foo) [with Foo = int]’:
main.cpp:5:7:   required from here
main.cpp:2:6: warning: unused parameter ‘f’ [-Wunused-parameter]
Marco
  • 849
  • 1
  • 7
  • 21
  • 3
    It is *part* of an error. You've missed some surrounding messages. – Joseph Mansfield Mar 26 '13 at 18:43
  • I think you removed the error when you reduced the code. See [here](http://ideone.com/qx5yxF). Please try providing an example that reproduces the error. – juanchopanza Mar 26 '13 at 18:43
  • The code in the question is valid and should compile without errors/warnings. The message you are pointing is part of a larger message, the previous line(s) contained a warning/error and this continues explaining how the compiler went into that warning/error – David Rodríguez - dribeas Mar 26 '13 at 18:46
  • @DavidRodríguez-dribeas No, I only get three lines, the two lines I posted and one warning about the unused parameter. – Marco Mar 26 '13 at 18:49
  • Can you post the output of the compiler exactly as shown? (If you are using an IDE, make sure to get the info from the window that the compiler runs, not from any problems/issues window). – David Rodríguez - dribeas Mar 26 '13 at 18:50

1 Answers1

27
main.cpp: In instantiation of ‘void fkt(Foo) [with Foo = int]’:
main.cpp:5:7:   required from here
main.cpp:2:6: warning: unused parameter ‘f’ [-Wunused-parameter]

This is all one warning. You are getting a 3 line warning about an unused parameter. The first two lines are the compiler attempting to help you identify the cause of the warning. Here's an English translation:

In the instantiation of fkt with template argument Foo as int which was required by line 5 column 7, you have an unused parameter called f.

fkt is a function template. Templates have to be instantiated with the given template arguments. For example, if you use fkt<int>, the fkt function template is instantiated with Foo as int. If you use fkt<float>, the fkt function template is instantiated with Foo as float.

In particular, this first line of this message is telling you that the warning occurs inside fkt which was instantiated with Foo as int. The second line of the warning tells you that instantiation occurred on line 5. That corresponds to this line:

fkt(1);

This is instantiating fkt with Foo as int because the template argument Foo is being deduced from the type of the argument you're giving. Since you're passing 1, Foo is deduced to be int.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • So, actually my code is 100% correct and there is no error and I can ignore this message as far as I understand. – Marco Mar 26 '13 at 18:56
  • @Marco Yes, it's fine. Except for the unused parameter. – Joseph Mansfield Mar 26 '13 at 18:57
  • You can ignore warnings, and this is a warning. However you should not. There are other and better possibilities. Either let the compiler ignore unused parameters (`-Wno-unused-parameter`), or use the parameter, or don't specify a name for it (which explicitly says the compiler that you won't use it) or by casting it to void, a common alternative way to tell the compiler that you're not going to use it (`(void)f;` as the first line of the function). – leemes Mar 26 '13 at 18:58
  • I wrote only part of the function by now, that's why the parameter is not used, yet. – Marco Mar 26 '13 at 19:01
  • 1
    Then it's totally safe to ignore this warning. However, when you're done implementing a function and don't use a parameter (maybe since you overwrite a virtual function of a base class and don't need the parameter), use one of the suggestions from above. It's not nice to keep warnings living in your project; you'll loose the overview and might ignore important warnings in the future. – leemes Mar 26 '13 at 19:04
  • @sftrabbit I'm astonished about the pace on SO. Usually I wait for a day or two until I accept (but I also wait for hours or days for an answer). Thanks for taking the time to look into this. – Marco Mar 26 '13 at 19:09