在C+ 11中是否可以做到这一点?一个具有相同名称的课程,但一个带有模板。例如:结果和结果< t>
这在C ++ 11中是否可以?两个类同名的类,但一个带有模板。 ex:
一个具有名称结果
的类,另一个带有名称结果< t>
return Result("Message");
:
Result<Date>("Message", date);
的类,我尝试使用此操作而没有成功
template<>
class response
{
public:
bool sucess;
std::string message;
int code;
};
template<typename T>
class response<T>
{
public:
T data;
};
Is this possible in c++11? Two class with same name but one with template.
Ex:
A class with name Result
and another with name Result<T>
to use like
return Result("Message");
or
Result<Date>("Message", date);
For example, I tried this without success:
template<>
class response
{
public:
bool sucess;
std::string message;
int code;
};
template<typename T>
class response<T>
{
public:
T data;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一对C ++ 11选项:
提供
void
的默认模板参数,并专门研究。然后
响应&lt;&gt;
将表示响应&lt; void&gt;
,并且不会具有data
成员。但是,您仍然必须编写响应&lt;&gt;(“ message”)
而不是wendesp(“ message”)
(在您的实际代码中,您可能想要给出
响应
构造函数,以便可以这样直接初始化它们。)您可以使用一个选项来增强此功能,因为函数过载集可以同时包含非模拟功能和功能模板。因此,例如,您可以拥有一个
make_response
函数:然后
make_response(“ message”)
将使respons&lt;&lt;&gt;
aka aka a <代码>响应&lt; void&gt; 没有data
成员,make_response(“消息”,日期)
将使响应&lt; date&gt;
具有一个。A couple C++11 options:
Provide a default template argument of
void
and specialize on that.Then
response<>
will meanresponse<void>
and will not have adata
member. However, you'd still have to writeresponse<>("Message")
instead ofresponse("Message")
(In your actual code you'd probably want to give
response
constructors so that they could be directly initialized like this.)One option you can use to augment this is with factory functions, because a function overload set can contain both non-templated functions and function templates. So, for instance, you can have a
make_response
function:Then
make_response("Message")
will make aresponse<>
aka aresponse<void>
that has nodata
member, andmake_response("Message", date)
will make aresponse<Date>
that has one.