I apologize for not having time enough to make a deep investigation and relying on your help instead.
Consider the simple code:
#include <iostream>
enum class PrintColour
{
COLOUR_1 = 0,
COLOUR_2 = 1,
};
void colour( auto c = PrintColour::COLOUR_1 )
{
switch ( c )
{
case PrintColour::COLOUR_1:
std::cout << "Colour 1" << std::endl;
break;
case PrintColour::COLOUR_2:
std::cout << "Colour 2" << std::endl;
}
}
int main( )
{
// colour( ); couldn't deduce template parameter ‘auto:1’
colour( PrintColour::COLOUR_1 ); // Fine!
}
This code exactly as it is compiles and runs without a problem. If I uncomment the colour( );
, though, g++ fires the error:
auto_param.cpp: In function ‘int main()’:
auto_param.cpp:27:10: error: no matching function for call to ‘colour()’
colour( );
^
auto_param.cpp:13:6: note: candidate: template<class auto:1> void colour(auto:1)
void colour( auto c = PrintColour::COLOUR_1 )
^~~~~~
auto_param.cpp:13:6: note: template argument deduction/substitution failed:
auto_param.cpp:27:10: note: couldn't deduce template parameter ‘auto:1’
colour( );
^
It is possible that I am just missing a silly point, or it is possible that I am really stupid and misunderstood the whole thing.
Should I be able to declare a function parameter as auto
while still being able to give it a default value in C++11 or C++14?
I thought the given default value would be enough to let compiler deduce the parameter type...
EDIT 1:
It think I need to make my question clearer so it won't be mistaken by Is there a way to pass auto as an argument in C++?
The point here is not passing auto
to a function, but having auto
in conjunction with a default value for the argument, something not considered in the aforementioned question.
EDIT 2:
As clarified in comments here, C++11 does not have such a feature of passing auto
as parameter, but C++14 and on (g++ 6.3.1 defaults to "gnu++14") seem to. My original question is not related to C++11, though, and my question is not whether C++11 supports auto
parameters. I was relying on auto
as parameter, but forgot to double check the minimum standard version for it. My apologies and I fixed that now.
g++ -std=c++11 auto_param.cpp -o auto_param
auto_param.cpp:13:14: error: use of ‘auto’ in parameter declaration only available with -std=c++14 or -std=gnu++14
I hope it to be clear the difference between my question and Is auto as a parameter in a regular function a GCC 4.9 extension?. Please tell me if not.