Creating an instance of the constructor with default arguments

I have a prototype constructor that looks like this:

template <typename type_position> window( const int size[2], const char* caption="Window", const SDL_Surface* icon=NULL, bool fullscreen=false, bool vsync=true, bool resizable=false, int multisample=0, type_position position=type_position(0) ) 

Then I want to create an instance:

 new window(screen_size,"My Window",NULL,fullscreen); 

The problem (I assume) is that T cannot be explicitly specified (that is, it can be int or long or short , etc.). I get an error message:

error C2660: window: the function does not accept 4 arguments

Then I tried to specify the type:

 new window<int>(screen_size,"My Window",NULL,fullscreen); 

But this does not work:

error C2512: window: no corresponding default constructor available
error C2062: enter "int" unexpectedly

I did some research, and how closest I could get it looked like the question " default value for C ++ template template , except that in my case, the template parameter can be inferred from the first argument.

So, am I stuck or is there something I am missing?

+4
source share
2 answers

You cannot provide an explicit list of template arguments for the constructor, and the template parameter cannot be inferred from the function argument by default, so the type_position position function parameter must be explicitly specified (not set by default) to infer the type.

Since this is the last parameter, it does not allow the use of any of the default arguments for contructor. You can reorder the constructor options to specify type_position first, or you can add a dummy argument that allows it to be output:

 template <typename type_position> window( type_position dummy, const int size[2], const char* caption="Window", const SDL_Surface* icon=NULL, bool fullscreen=false, bool vsync=true, bool resizable=false, int multisample=0, type_position position=type_position(0) ); 

Then call it using the dummy first parameter of the inference type:

 new window(1, screen_size,"My Window",NULL,fullscreen); 

Alternatively, if you are using C ++ 11, you can specify a default template argument:

 template <typename type_position = int> window( const int size[2], const char* caption="Window", const SDL_Surface* icon=NULL, bool fullscreen=false, bool vsync=true, bool resizable=false, int multisample=0, type_position position=type_position(0) ); 

Alternatively, decide if you really need a template constructor with the parameter you want to output. What do you plan to do with type_position if you donโ€™t know what is ahead of time? Does someone really call this constructor with std::string as the position parameter? Or a vector<double> ? This may make sense, depending on what your type does, but it does not always make sense.

+2
source

The more I thought about this, it seems you just need to provide a separate constructor:

 window( const int size[2], const char* caption="Window", const SDL_Surface* icon=NULL, bool fullscreen=false, bool vsync=true, bool resizable=false, int multisample=0, int position=0 ) 
0
source

Source: https://habr.com/ru/post/1414785/


All Articles