[英]Constructor template instantiation with default arguments
I have a constructor prototype that looks like: 我有一个构造函数原型,如下所示:
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)
)
I then want to construct an instance: 然后,我想构造一个实例:
new window(screen_size,"My Window",NULL,fullscreen);
The issue (I assume) is that T
cannot be specified explicitly (ie, it could be int
or long
or short
, etc.). 问题(我认为)是
T
不能明确指定(即,它可以是int
或long
或short
等)。 I get the error: 我得到错误:
error C2660: 'window' : function does not take 4 arguments
错误C2660:“窗口”:函数未采用4个参数
I then tried to specify the type: 然后,我尝试指定类型:
new window<int>(screen_size,"My Window",NULL,fullscreen);
But that doesn't work: 但这不起作用:
error C2512: 'window' : no appropriate default constructor available
错误C2512:“窗口”:没有合适的默认构造函数
error C2062: type 'int' unexpected错误C2062:意外键入'int'
I've done some research, and about the closest I could get was similar to that is the question " C++ template function default value ", except that in my case, the template parameter can be inferred from the first argument. 我已经进行了一些研究,关于我能得到的最接近的问题是“ C ++模板函数默认值 ”问题,除了在我的情况下,可以从第一个参数推断出模板参数。
So, am I stuck or is there something I'm missing? 那么,我被卡住了还是缺少什么?
You can't a provide explicit template argument list for a constructor, and a template parameter cannot be deduced from a default function argument, so the type_position position
function parameter needs to be provided explicitly (not defaulted) in order to deduce the type. 您无法为构造函数提供明确的模板参数列表,并且无法从默认函数参数推导出模板参数,因此需要显式提供
type_position position
函数参数(未默认设置)以推导类型。
As that is the final parameter, it prevents you using any of the contructor's default arguments. 因为这是最后一个参数,所以它防止您使用任何构造函数的默认参数。 You could re-order the constructor parameters so the
type_position
is given first, or you could add a dummy argument that allows it to be deduced: 您可以重新排序构造函数的参数,以便首先给出
type_position
,或者可以添加一个伪参数来推导它:
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 with a dummy first parameter of the type to be deduced: 然后使用要推导的虚拟第一个参数调用它:
new window(1, screen_size,"My Window",NULL,fullscreen);
Alternatively, if you're using C++11, you can provide a default template argument: 另外,如果您使用的是C ++ 11,则可以提供一个默认的模板参数:
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 want a template constructor with a parameter that needs to be deduced. 或者,确定您是否真的想要带有需要推导参数的模板构造函数。 What do you plan to do with the
type_position
type if you don't know what it is in advance? 如果您事先不知道
type_position
类型,您打算怎么做? Is it valid for someone to call that constructor with a std::string
as the position
parameter? 有人用
std::string
作为position
参数调用该构造函数是否有效? Or a vector<double>
? 还是
vector<double>
? It might make sense, depending on what your type does, but it doesn't always make sense. 这可能很有意义,具体取决于您的类型,但这并不总是很有意义。
The more I thought about it, it looks like 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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.