【问题标题】:Function specialization to access struct members with getter using enum使用枚举通过 getter 访问结构成员的函数特化
【发布时间】:2021-01-21 19:07:44
【问题描述】:

我有一个枚举和一个结构

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

}

我想用一个专门的 get 函数访问它的成员,基本上看起来像这样

T get(STORE_ENUM,store s);

它返回适当的类型并希望静态类型检查。 这在 C++ 中可行吗?

【问题讨论】:

  • 在 c++17 中你可以使用 if constexpr - 但签名必须是 template <StoreEnum kind> auto& get(Store&); 但是你用 C++11 标记了问题

标签: c++ c++11 templates


【解决方案1】:

是的,这是可能的。 boost PFR 库允许的内容与您的设想非常相似,例如:

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

参见教程here。

【讨论】:

    【解决方案2】:

    使用 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";
    }
    

    见https://godbolt.org/z/1vffh3

    【讨论】:

    • 这个问题是它没有类型检查不正确的用法。 float f = get<:a_data>(store);
    • @FamousJameis 即使类型是明确的,问题仍然存在。要克服这种情况,请使用 float f {get&lt;STORE_ENUM::A_DATA&gt;(store)};
    【解决方案3】:

    在我的解决方案中,我们不需要枚举。它使用模板。

    #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;
    }
    

    输出

    63
    63
    63
    @
    65
    true
    

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

    在https://repl.it/@JomaCorpFX/FunctionSpecialization#main.cpp中检查/运行此代码

    【讨论】:

      【解决方案4】:

      std::tuple 基本上是这样做的,而你的类型基本上是一个元组。所以 easy 和 fast 的方法是重用 std::tuple 的机器。

      在 中可能如下所示:

      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;
      }
      

      这是,但 的缺失位很简单:

      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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-15
        • 2019-03-02
        相关资源
        最近更新 更多