1

Three simple statements, one does not compile.

std::vector<size_t>({});
std::vector<size_t>({ 1 });  // This does not compile
std::vector<size_t>({ 1, 2 });

Only the second statement with one element in the list brings an error:

cannot convert from 'initializer-list' to 'std::vector<size_t,std::allocator<char32_t>>'

It works fine for char, long, int etc., but not for size_t. Does anybody know why and maybe there is a workaround?

Here is the complete code:

// ConsoleApplication1.cpp : main project file.

#include "stdafx.h"
#include <vector>

using namespace System;

int main(array<System::String ^> ^args)
{
    std::vector<size_t>({});
    std::vector<size_t>({ 1 });
    std::vector<size_t>({ 1, 2 });
    return 0;
}

I'm using the C++/CLI compiler from VS2013

Guillaume Pascal
  • 845
  • 1
  • 9
  • 19
Wernfried
  • 206
  • 3
  • 9

1 Answers1

1

The right and proper way to use std::initializer-list is

    std::vector<size_t>{};
    std::vector<size_t>{ 1 };
    std::vector<size_t>{ 1, 2 };

Ie. Without parenthesis. See http://www.stroustrup.com/C++11FAQ.html#init-list for examples.

Using this should fix your issue. Otherwise, in last resort, you can force type like this: std::vector<size_t>( std::initializer-list<size_t>{ 1 } );

Guillaume Pascal
  • 845
  • 1
  • 9
  • 19