mercredi 26 avril 2017

C++11 auto type deduction for argument with default value

I apologise 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...

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire