如何编写一个类模板来处理不同的数据成员?

huangapple go评论69阅读模式
英文:

How to write a class template to handle different data members?

问题

I would like to write a class template that behaves as follows:

  • If I pass Type_1 as an argument of the class instance, having then something like TestClass<MessageType::Type_1> t1, I want to see both members of the struct Message.

  • If I pass Type_2, I want to see only the integer member_2 and NOT the member_1 inside the Message struct.

So, in a nutshell, I would like to decide what are the members according to the template parameter.

#include <string>

enum class MessageType
{
    Type_1,
    Type_2
};

template <MessageType messageType>
class TestClass
{
public:
    struct Message
    {
        std::string member_1;
        int member_2{0};
    };     
};

I've tried to use conditional statements but I got compiler errors.

英文:

I would like to write a class template that behaves as follows:

  • If I pass Type_1 as an argument of the class instance, having then something like TestClass<MessageType::Type_1> t1, I want to see both memebers of the struct Message.

  • If I pass Type_2, I want to see only the integer member_2 and NOT the member_1 inside the Message struct.

So, in a nutshell, I would like to decide the what are the members according to the template parameter.

#include <string>

enum class MessageType
{
    Type_1,
    Type_2
};

template <MessageType messageType>
class TestClass
{
public:
    struct Message
    {
        std::string member_1;
        int member_2{0};
    };     
};

I've tried to use conditional statements but I got compiler errors

答案1

得分: 1

The provided code snippet is as follows:

无法确定以下内容在您的情况下是否可行。如果可能的话,我会重构为具有两种不同类型,然后通过单独模板的专门化来选择其中一种:

#include <iostream>
#include <string>

enum class MessageType
{
    Type_1,
    Type_2
};

struct message1 { std::string member1; int member2; };
struct message2 { std::string member1; };

template <MessageType mt> struct select_type;
template <> struct select_type<MessageType::Type_1> { using type = message1; };
template <> struct select_type<MessageType::Type_2> { using type = message2; };


template <MessageType select>
struct TestClass
{
    using message_type = typename select_type<select>::type;
};

int main()
{
    return 0;
}

如果您需要进一步的帮助,请告诉我。

英文:

Whether the following is viable in your case depends. If possible I would refactor to have two different types and then pick one via specialization of a separate template:

#include &lt;iostream&gt;
#include &lt;string&gt;

enum class MessageType
{
    Type_1,
    Type_2
};

struct message1 { std::string member1; int member2; };
struct message2 { std::string member1; };

template &lt;MessageType mt&gt; struct select_type;
template &lt;&gt; struct select_type&lt;MessageType::Type_1&gt; { using type = message1; };
template &lt;&gt; struct select_type&lt;MessageType::Type_2&gt; { using type = message2; };


template &lt;MessageType select&gt;
struct TestClass
{
    using message_type = typename select_type&lt;select&gt;::type;
};

int main()
{
    return 0;
}

huangapple
  • 本文由 发表于 2023年4月17日 19:32:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定