【问题标题】:Is it possible to have an "auto" member variable?是否可以有一个“自动”成员变量?
【发布时间】:2013-08-13 02:31:32
【问题描述】:

例如,我想要一个auto 类型的变量,因为我不确定它是什么类型。

当我尝试在类/结构声明中声明它时,它给了我这个错误:

无法推断自动类型。需要初始化器

有办法解决吗?

struct Timer {

    auto start;

};

【问题讨论】:

  • 这不是模板的用途吗?
  • 你什么时候知道它是什么类型的?编译时还是运行时?你能给我们一些背景吗?你想做什么?
  • 什么是sizeof(Timer)?
  • 这是什么意思?
  • @Oleksiy 请提供更多描述。为什么你不能决定它的类型?

标签: c++ struct auto


【解决方案1】:

可以,但必须声明static 和const:

struct Timer {
    static const auto start = 0;
};

A working example in Coliru.

有了这个限制,您因此不能将start 作为非静态成员,并且不能在不同的对象中有不同的值。

如果你想要不同类型的start 用于不同的对象,最好将你的类作为模板

template<typename T>
struct Timer {
    T start;
};

如果你想推断T的类型,你可以做一个类似工厂的函数来做类型推断。

template<typename T>
Timer<typename std::decay<T>::type> MakeTimer(T&& startVal) {   // Forwards the parameter
   return Timer<typename std::decay<T>::type>{std::forward<T>(startVal)};
}

Live example.

【讨论】:

    【解决方案2】:

    这是C++ draft standard 在7.1.6.4 auto specifier 段落4 中对成员变量使用auto 所说的话:

    auto 类型说明符也可用于在选择语句 (6.4) 或迭代语句 (6.5) 的条件中,在 new-type-id 或 type 中的 type-specifier-seq 中声明变量新表达式 (5.3.4) 的 -id,在 for-range-declaration 中,以及 在声明带有出现在类的成员规范中的大括号或等号初始化器的静态数据成员时定义 (9.4.2).

    由于它必须被初始化,这也意味着它必须是const。因此,以下内容将起作用:

    struct Timer
    {
      const static int start = 1;
    }; 
    

    不过,我认为这不会让你感到太多。使用 Mark 建议的模板,或者现在我想多了,也许你只需要一个variant type。在这种情况下,您应该查看Boost.Variant 或Boost.Any。

    【讨论】:

      【解决方案3】:

      如果你在 lambda 表达式中声明一个类,你可以使用decltype推断成员变量的类型:

      #include <iostream>
      #include <vector>
      #include <string>
      using namespace std;
      
      auto generic_class = [](auto width1, auto height1) {
          class local_class {
              public:
              decltype(width1) width;
              decltype(height1) height;
          } local;
          local.width = width1;
          local.height = height1;
          return local;
      };
      
      int main()
      {
          auto obj1 = generic_class(3,std::string("Hello!"));
          auto obj2 = generic_class(std::vector<int>{1,2},true);
          cout << obj1.height << "\n";
          cout << obj2.width[0] << "\n";
          return 0;
      }
      

      【讨论】:

        【解决方案4】:

        没有。每个构造函数都可以有自己的 start 初始化器,因此可能没有一致的类型可供使用。

        如果你确实有一个可用的表达式,你可以使用它:

        struct Timer {
        
           Foo getAFoo();
        
           delctype(Timer().getAFoo().Bar()) start;
        
           Timer() : start(getAFoo().Bar()) { /***/ }
        };
        

        【讨论】:

        • 有什么方法可以写这个而不重复getAFoo().Bar()?
        • @Eric:当然,只需添加一个成员 auto getAFooBar() { return getAFoo().Bar(); } 并使用它。该示例展示了更复杂的变体仍然有效。
        【解决方案5】:

        间接地,前提是您不引用该类的成员。

        现在这也可以通过推导指南来实现,这些是在 C++17 中引入的,最近(最终)添加了对 VC++ 的支持(clang 和 GCC 已经有了)。

        https://en.cppreference.com/w/cpp/language/class_template_argument_deduction

        例如:

        template <typename>
        struct CString;
        
        template <typename T, unsigned N>
        struct CString<std::array<T, N>>
        {
            std::array<T, N> const Label;
        
            CString(std::array<T, N> const & pInput) : Label(pInput) {}
        };
        
        template <typename T, std::size_t N>
        CString(std::array<T, N> const & pInput) -> CString<std::array<T, N>>;
        

        https://godbolt.org/z/LyL7UW

        这可用于以类似于 auto 的方式推断类成员类型。虽然成员变量需要以某种方式依赖于构造函数参数。

        【讨论】:

        • 这似乎并不相关,真的。这里没有扣除会员的类型。相反,该成员是显式类型的,尽管碰巧所述类型取决于模板参数。这里所做的是从构造函数调用中推断出那些模板参数,而不是成员的类型。成员的类型由类的类型决定;它根本不是自动的。
        • 成员类型是从推演指南推导出来的,没有明确类型。问题特别指出“我想要一个 auto 类型的变量,因为我不确定它是什么类型。”扣除指南解决了这个问题,前提是构造函数可以用于扣除(这很可能但不是garenteed)。这实际上类似于评分最高的答案工厂功能。所以不仅仅是我认为这可能有助于提问者。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多