Is vector not allowed ? (error: invalid use of ‘auto’)

后端 未结 2 842
情书的邮戳
情书的邮戳 2020-12-20 16:24

I have:

#include 
#include 

using namespace std;

int main()
{
   auto a = -SOME_CONST_MAX;
   vector myVec {a, a,          


        
2条回答
  •  天命终不由人
    2020-12-20 16:49

    If I remember correctly, proposals have been made for vector syntax. They where not accepted by the C++ standard committee.

    C++17 will introduce something like std::vector bob = {a,a,a,a}; that just works. Note the lack of . This may just be the language feature, with actual use in std following afterward.

    auto is also added to templates, but auto is always a value never a type. So using auto to replace a type was considered a bad idea.

    Here is a use of auto in a template:

    template
    using constant_t=std::integral_constant;
    template
    constexpr constant_t constant{};
    

    and now constant<7> is a std::integral_constant. This is considered useful for many reasons.

    The answer to your practical problem using current C++ is:

    auto a = -SOME_CONST_MAX;
    std::vector myVec {a, a, a, a};
    

    where we deduce the type of a and pass it to vector.

提交回复
热议问题