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

后端 未结 2 846
情书的邮戳
情书的邮戳 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:39

    I find Slava's solution really simple and elegant

    vector myVec {a, a, a, a};
    

    But just to show another way, you can use the variadic template function

    template 
    std::vector getVect (T const & t, Ts const & ... ts)
     { return { t, ts... } ; }
    

    You can use auto again

    auto myVec = getVect(a, a, a, a, a);
    

提交回复
热议问题