简体   繁体   English

Function 专门用于使用枚举使用 getter 访问结构成员

[英]Function specialization to access struct members with getter using enum

I have an enum and a struct我有一个枚举和一个结构

enum STORE_ENUM { A_DATA, B_DATA, C_DATA, D_DATA };
struct Store {
   int a;
   char b;
   long c;
   bool d;

}

and I want to access its members with a specialized get function that basically looks like this我想用一个专门的 get function 来访问它的成员,基本上看起来像这样

T get(STORE_ENUM,store s);

and it returns the appropriate type and hopefully statically type checks.它返回适当的类型,并希望静态类型检查。 is this possible in C++?这可能在 C++ 中吗?

Yes it's possible.是的,这是可能的。 The boost PFR library allows something very similar to how you've envisaged it, eg: boost PFR 库允许的内容与您的设想非常相似,例如:

auto& x = boost::pfr::get<B_DATA>(s);

See the tutorial here .请参阅此处的教程。

With C++17, you can do the following使用 C++17,您可以执行以下操作

#include <iostream>

enum struct STORE_ENUM { A_DATA, B_DATA, C_DATA, D_DATA };
struct Store {
   int a;
   char b;
   long c;
   bool d;

};

template<STORE_ENUM storeEnum>
auto get(Store const & s){
    if constexpr (storeEnum == STORE_ENUM::A_DATA) return s.a;
    if constexpr (storeEnum == STORE_ENUM::B_DATA) return s.b;
    if constexpr (storeEnum == STORE_ENUM::C_DATA) return s.c;
    if constexpr (storeEnum == STORE_ENUM::D_DATA) return s.d;
}
int main(){
    auto store = Store{ 0, 'a', 4l, true};
    std::cout << get<STORE_ENUM::A_DATA>( store) << "\n";
    std::cout << get<STORE_ENUM::B_DATA>( store) << "\n";
    std::cout << get<STORE_ENUM::C_DATA>( store) << "\n";
    std::cout << get<STORE_ENUM::D_DATA>( store) << "\n";
}

See https://godbolt.org/z/1vffh3https://godbolt.org/z/1vffh3

In my solution, we don't need enums.在我的解决方案中,我们不需要枚举。 It uses templates.它使用模板。

#include <iostream>
#include <type_traits>

struct Store
{
    int a;
    char b;
    long c;
    bool d;

    Store() //Default constructor
    {
        a = 0;
        b = 0;
        c = 0;
        d = false;
    }

    Store(int a, char b, long c, bool d) //Constructor. Custom values.
    {
        this->a = a;
        this->b = b;
        this->c = c;
        this->d = d;
    }

    template <typename T = int,
              typename = typename std::enable_if<std::is_same<T, int>::value ||
                                                     std::is_same<T, char>::value ||
                                                     std::is_same<T, long>::value ||
                                                     std::is_same<T, bool>::value,
                                                 void>::type>
    T GetData()
    {
        if (std::is_same<T, char>::value)
        {
            return b;
        }
        if (std::is_same<T, long>::value)
        {
            return c;
        }
        if (std::is_same<T, bool>::value)
        {
            return d;
        }
        return a;
    }
};

int main()
{
    Store store { 63, '@', 65, true };
    std::cout << store.GetData() << std::endl;
    std::cout << store.GetData<>() << std::endl;
    std::cout << store.GetData<int>() << std::endl;
    std::cout << store.GetData<char>() << std::endl;
    std::cout << store.GetData<long>() << std::endl;
    std::cout << std::boolalpha << store.GetData<bool>() << std::endl;
}

Output Output

63
63
63
@
65
true

Compile clang++./main.cpp -std=c++11 or g++./main.cpp -std=c++11编译clang++./main.cpp -std=c++11g++./main.cpp -std=c++11

Check/run this code in https://repl.it/@JomaCorpFX/FunctionSpecialization#main.cpphttps://repl.it/@JomaCorpFX/FunctionSpecialization#main.cpp中检查/运行此代码

std::tuple basically does this, and your type is basically a tuple. std::tuple基本上是这样做的,而您的类型基本上是一个元组。 So the easy and fast way is to just reuse std::tuple 's machinery.所以简单快捷的方法是重用std::tuple的机器。

In it might look like this:,它可能如下所示:

template<STORE_ENUM e>
auto get( Store s ){
  return std::get<(unsigned)e>(std::make_tuple(s.a,s.b,s.c,s.d));
}
template<STORE_ENUM e, class T>
void set( Store& s, T t ){
  std::get<(unsigned)e>(std::tie(s.a,s.b,s.c,s.d))=t;
}

This is , but the missing bit for is easy:这是 ,但的缺失位很容易:

template<STORE_ENUM e>
using store_type = typename std::tuple_element<(unsigned)e, std::tuple<int,char,long,bool>>::type;

template<STORE_ENUM e>
store_type<e> get( Store s ) {
  return std::get<(unsigned)e>(std::make_tuple(s.a,s.b,s.c,s.d));
}

template<STORE_ENUM e>
void set( Store& s, store_type<e> v ){
  std::get<(unsigned)e>(std::tie(s.a,s.b,s.c,s.d))=v;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM