Go would do:
v := []TYPEHERE{1, 2, 3}
where TYPEHERE is the type of array[1] that you're creating. Is there any equivalent C++ technique here? Explaining all the machinery may blow out a novice's mind, but I think explaining "You need to give C++ some clue about the type" isn't so bad.[1]: Before you complain I just created a slice, remember the slice has to point at an array; I've created both a slice and an array so it's not "wrong" to say I've created an array with that syntax.
Re:Popular Myths about C++, Part 1
by TheCoelacanth on Tuesday December 09, 2014 @11:25AM
(#8723627)
Yes, C++ has no shortage of ways to initialize variables. Any of these should work
TYPENAME v {1, 2, 3};
TYPENAME v = {1, 2, 3};
auto v = TYPENAME {1, 2, 3};The way most other languages handle it is that the list literal construct just creates a list, and if you want something else then you need to explicitly convert. For example, in Python:
x = [1, 2, 3, 5, 8, 13] # this is a list of integers
y = set([1, 2, 3, 5, 8, 13]) # this is a set of integers
There's nothing inherently wrong with C++'s initializer list approach. Nor is there anything inherently wrong with auto. But they collide in unfortunate ways because they haven't been designed such that they go well together. C++ is full of things like this. Responding with "well, that makes the most sense" for any given feature is missing the point entirely.> The way most other languages handle it is that the list literal construct just creates a list, and if you want something else then you need to explicitly convert. > For example, in Python:
> x = [1, 2, 3, 5, 8, 13] # this is a list of integers
> y = set([1, 2, 3, 5, 8, 13]) # this is a set of integers
Actually, that's not the best way to do it:
y = {1, 2, 3, 5, 8, 13} # this is a set of integers without creating a list first.
z = set(1, 2, 3, 5, 8, 13) # so is this
In C++ {} is syntactic sugar for "Create an initializer_list with these values," which is what happens when it's assigned to an "auto" variable. Not surprising at all. If you want a different type, you need to specify the type that you want.Maybe it's confusing to beginners and people who don't take the time to learn the language, but C++ isn't catering to those people.
Don't get me wrong, there are a lot of "gotchas" in C++, I just don't think the two you've posted are very bad.
Um, the whole point of this discussion is about the claim that C++ is a good language for beginners to learn.
I have problems with using C++ to build real projects with experienced programmers too, but those are mostly different problems.
Re:Popular Myths about C++, Part 1
by maxlybbert on Tuesday December 09, 2014 @10:50AM
(#8723381)
Well, to be fair, with "auto x = { 1, 2, 3, 4, 5, 6}" you will get a variable named "x", but it won't be a std::vector<int> (it'll be a std::initializerList<int>).
> Kaboom. Oops. You can use auto, or you can use {} to make a vector, but you can't do both at the same time!
IMO it would be more confusing if that created a vector<int>. Unless the compiler is reading your mind, how is it supposed to know you meant a vector<int>, a set<int>, deque<int>, a struct { int a,b,c,d,e,f}, or some other object who's constructor can take 6 ints?