How to implement an initializer list for user defined type? (analogus to std::vector initializer list)

Create a constructor which takes a std::initializer_list as a parameter:

#include <vector>
#include <initializer_list>

template <typename T>
struct foo
{
private:
    std::vector<T> vec;

public:

    foo(std::initializer_list<T> init) 
      : vec(init)
    { }
};


int main()
{
    foo<int> f {1, 2, 3, 4, 5};
}

std::vector does this is almost exactly the same way (although utilizing begin() and end() - std::initializer_list has iterators much the same way as other containers do). From gcc:

  vector(initializer_list<value_type> __l,
     const allocator_type& __a = allocator_type())
  : _Base(__a)
  {
_M_range_initialize(__l.begin(), __l.end(),
            random_access_iterator_tag());
  }

Edit: I'm not 100% what you're trying to do, but you may simply be able to use uniform initialization to get what you want:

struct bar
{
private:

    int i;
    double j;
    std::string k;

public:

    bar(int i_, double j_, const std::string& k_)
      : i(i_), j(j_), k(k_)
    { }

};

int main()
{
    bar b {1, 2.0, "hi"};
}