Do you use C++11

Pages: 123456... 8
Wouldn't it have also been an issue introducing other keywords such as constexpr?
Yes, but to a much lesser extent, "constexpr" doesn't come up very much, whereas words like foreach,then,elseif, elif are common and C++ programmers sometimes feel inclined to emulate them.
1
2
3
4
for(auto &v : mycontainer)
{
    v += blah;
}


This is not a problem if you have an IDE that can give you accurate type hints when you put your mouse cursor over the variable name.

I wonder how many C++ IDEs would guess the correct type of v here, especially if mycontainer is a user supplied container template. My rough guess for 2013 is: 0.

Last edited on
Change that to 1, I'm not even kidding:
http://i.imgur.com/2AWTqcV.png

Note: I'm using a plugin called sublime-clang:
https://github.com/quarnster/SublimeClang
Last edited on
Ok, so it gets a trivial case fine, but what about "container template".

Edit: Seems like they discontinued sublime plugin. So this essentially counts as 0.5, not 1. :D
Last edited on
rapidcoder wrote:
Ok, so it gets a trivial case fine, but what about "container template".
Yes, that works too, not sure why you thought that would add complexity.

And wow, I was not aware that it was discontinued. Oh well.
Last edited on
rapidcoder:
Make that 2.
VS2012 seems to find out which type it really is, just copy'n'pasted LB's example.
The trick is that it uses clang's compiler ABI and it literally asks clang to deduce the type - it's not traditional intellisense, it's actually asking a compiler to examine the code and figure it out like it actually would to compile the code. Any level of complexity you could add would be meaningless, so long as it compiled :p
L B:
Wonder how Microsoft does it...

Anyway, OnT:
Yep, sometimes I do use C++11 stuff. But it feels like it is time to make a change from VS2012 and msvc to something else...

For example;
1
2
3
4
5
6
7
8
9
10
11
class foo
{
public:
    foo() : foo(42) {}
    foo(int var)
    {
        _var = var;
    }
private:
    int _var;
};


Doesn't work with msvc11... you have to call it like:
this->foo::foo(42);
Instead, which doesn't compile in any other compiler than msvc11 x)
MSVC is behind: http://msdn.microsoft.com/en-us/library/vstudio/hh567368.aspx
I think more recent versions support more stuff, but that page has not been updated as far as I know.

BOT: I do really enjoy using lots of C++11 features - not just for the heck of it, but because I actually find them useful. One thing I really like is the ability to do this:
1
2
3
4
for(auto &v : {ScopedEnum::A, ScopedEnum::B, ScopedEnum::D})
{
    //...
}
That used to be painful in C++03 (not talking about scoped enums).

Real world example: https://github.com/LB--/ChessPlusPlus/blob/lb-refactor/src/board/Queen.cpp#L21
closed account (zb0S216C)
LB wrote:
"Are you sure? This looks pretty obfuscated to me:"

I prefer the first "for" variation. Just by looking at it, I can tell (and control) when the loop ends and I can also tell the size of the iteration step (which I can also control). With the range-based "for", however, I can only see limited information (reduced functionality and more obscurity).

I understand that the range-based "for" is an alternative way of iterating through a container and not a replacement; just to be clear on that.

LB wrote:
".begin() and .end() don't work on arrays."

That's the whole point of "std::array" (which I do actually find useful, but I don't actually use it).

Fredbill30 wrote:
"I don't find 95 percent of the standard library useful actually."

I agree. The only standard things I actually use in my projects are, well... "malloc( )/free( )".

Wazzak
Last edited on
Framework wrote:
I prefer the first "for" variation. Just by looking at it, I can tell (and control) when the loop ends and I can also tell the size of the iteration step (which I can also control).
My mistake, I forgot to explain that we are discussing a scenario where you need to iterate all elements in a container. If you need to iterate a part of a container, consider: http://ideone.com/uFgF6S - this allows you to abstract it to passing the range to another function - instead of saying "hey, take the whole thing and work with this range", you say "take this range and work with it". Much more concise - generally cuts out two parameters and ensures that the non-present range is not affected. You can't make only specific parts of a container constant.
Framework wrote:
With the range-based "for", however, I can only see limited information (reduced functionality and more obscurity).
What is obscured? It is clear that it iterates over all elements in the container, is it not?
Framework wrote:
I understand that the range-based "for" is an alternative way of iterating through a container and not a replacement; just to be clear on that.
It should be the first instinct when you think "I need to do something for each x in y".

Framework wrote:
That's the whole point of "std::array" (which I do actually find useful, but I don't actually use it).
Not when you have a container passed to a templated function - are you really going to specialize for arrays and convert them to std::array first?

Framework wrote:
I agree. The only standard things I actually use in my projects are, well... "malloc( )/free( )".
Can you link me to some of your projects? I want to see how you manage without the standard library.
Last edited on
closed account (N36fSL3A)
@L B

Well if he programs low level apps for microprocessors, OS s, etc. Well he doesn't need to use anything from the std library.
"I don't find 95 percent of the standard library useful actually."
Give me an example when to use 10 things from the std library.
Threading. We are using non-standard library because we were using it befor C++11 and don't want to rewrite all code. But if you aren't using threading, you are using ½~⅛ of your CPU. It is very important for games. If only Dwarf fortress was rewritten using multithreading, it would give me more that 60 FPS with 250 dwarves.
and if you are using threads you probably need <functional>

tuples is pretty useful too. I often use std::tie when implementing relationship operators for data structures.

Different random number generators are prety useful too. I didn't need most of them but I like normal distribution generator and fact that we can have several independent generators we can seed independely.

static_assert and type_trait are used sometimes in templates.
Fixed width types when you need binary compatibility.

LB have already wrote about containers, so I will not repeat that.

And, of course, streams and strings.

Offtopic:
In C++14 variable length arrays will be supported. However sizeof() will not work with them.
Does anybody have information about std::begin/end on vararrays?
Because AFAIK for arrays std::begin(a) == a and std::end(a) == a + sizeof(a);

Also there is some C++14 stuff on http://en.cppreference.com/w/ already
closed account (N36fSL3A)
I think I'll still use 03.
std::end(a) is not defined in terms of sizeof, for an array of size N, it's array + N
I hope MiiNiPaa knew what he meant but wrote sizeof by mistake ;)
Err... Pretend that a was casted to void pointer in second case...
I know that that array size is a template parameter fo begin overload. I shoud have wrote size_of to prevent mixing it with operator.

Actually I won't think that it will be supported: even GCC implementation of vararrays does not support std::begin/end thought it supports sizeof() (which C++14 one will not)
That is probably because you should know array size in compile time to prevent decaying to pointer when passed to function or to use it as template parameter.
Last edited on
MiiNiPaa wrote:
Err... Pretend that a was casted to void pointer in second case...
static_cast<void *>(a) + sizeof(void *) doesn't make sense for finding the length of array a.

How does it support sizeof? o_o
1
2
3
4
5
6
7
8
9
10
11
void f(std::size_t s)
{
    int a[s];
    std::cout << sizeof(a) << std::endl;
}

int main()
{
    f(1);
    f(100);
}
What does it print?
Last edited on
I have updated post. It wasn't obvious enough that it was a joke.

I meant that to make my statement valid you shoud do static_cast<void *>(a) + sizeof(a)
I think you still haven't said what you meant. Pointer arithmetic is invalid on void pointers -- there is no associated type to determine the size of increments.
Pages: 123456... 8