C ++调用显式模板构造函数 [英] C++ invoke explicit template constructor

查看:148
本文介绍了C ++调用显式模板构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能告诉我如何显式调用模板构造函数(在初始化列表中)?
例如:

Can you tell me how to invoke template constructor explicitly (in initializer list)? for example:

struct T { 
    template<class> T();
};

struct U {
    U() : t<void>() {} //does not work
    T t;
};

感谢

推荐答案

这是不可能的。该标准还在 14.8.1 / 7


注意:因为显式模板参数列表跟在函数模板名称后面,并且因为在不使用函数名称的情况下调用转换成员函数模板和构造函数成员函数模板,所以无法为这些函数模板提供显式模板参数列表。 ]

[Note: because the explicit template argument list follows the function template name, and because conversion member function templates and constructor member function templates are called without using a function name, there is no way to provide an explicit template argument list for these function templates. ]

如果您可以使用它,您可以使用它

If you can live with it, you can work it around

struct T { 
    template<class U> T(identity<U>);
};

struct U {
    U() : t(identity<void>()) {}
    T t;
};

给定 identity / p>

Given identity like it's defined in boost

template<typename T> struct identity { typedef T type; };

这篇关于C ++调用显式模板构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆