在C+ 11中是否可以做到这一点?一个具有相同名称的课程,但一个带有模板。例如:结果和结果< t>

发布于 2025-02-12 10:38:24 字数 495 浏览 0 评论 0原文

这在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贪了杯 2025-02-19 10:38:25

一对C ++ 11选项:

提供void的默认模板参数,并专门研究。

template<class T = void>
class response
{
public:
    bool success;
    std::string message;
    int code;
    T data;
};

template<>
class response<void>
{
public:
    bool success;
    std::string message;
    int code;
};

然后响应&lt;&gt;将表示响应&lt; void&gt;,并且不会具有data成员。但是,您仍然必须编写响应&lt;&gt;(“ message”)而不是wendesp(“ message”)

(在您的实际代码中,您可能想要给出响应构造函数,以便可以这样直接初始化它们。)


您可以使用一个选项来增强此功能,因为函数过载集可以同时包含非模拟功能和功能模板。因此,例如,您可以拥有一个make_response函数:

// Assuming the appropriate response constructors exist
response<> make_response(std::string t_message)
{
    return response<>{t_message};
}

template<class T>
response<T> make_response(std::string t_message, T t_data)
{
    return response<T>{t_message, t_data};
}

然后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.

template<class T = void>
class response
{
public:
    bool success;
    std::string message;
    int code;
    T data;
};

template<>
class response<void>
{
public:
    bool success;
    std::string message;
    int code;
};

Then response<> will mean response<void> and will not have a data member. However, you'd still have to write response<>("Message") instead of response("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:

// Assuming the appropriate response constructors exist
response<> make_response(std::string t_message)
{
    return response<>{t_message};
}

template<class T>
response<T> make_response(std::string t_message, T t_data)
{
    return response<T>{t_message, t_data};
}

Then make_response("Message") will make a response<> aka a response<void> that has no data member, and make_response("Message", date) will make a response<Date> that has one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文