0
#include <vector>
#include <iostream>

class Obj{
    int a = 6;
};

void func(const std::vector<Obj>& a) {
    std::cout << "here" << std::endl;
}

void func(const Obj& a) {
    std::cout << "there" << std::endl;
}

int main() {
    Obj obj, obj2;
    func({obj});
    func({obj, obj2});
}

Expected output:

here
here

Actual output:

there
here

It seems {obj} doesn't initialize a vector, but rather an object. I guess there is some priority order when it comes to which type it initializes. How do I control it precisely?

(Examples compiled with g++ (Ubuntu 8.3.0-6ubuntu1) 8.3.0.)

qweruiop
  • 3,156
  • 6
  • 31
  • 55
  • That link doesn’t answer my question though: how do I control it more precisely? ie, how to use initializer list to unit a single item vector? – qweruiop Apr 16 '19 at 04:23
  • See follow-up question: https://stackoverflow.com/questions/55700589/use-initializer-list-to-create-single-item-vector/55709932 – andreee Apr 16 '19 at 15:15

0 Answers0