5

When initializing primitive types like int or pointers one can use either copy-initialization or direct-initialization.

int a = 10;
int b(10);

Although the latter way is preffered for objects with constructors, I don't see people using it for primitives. I understand that it is kind of "more natural" (especially for numbers) to use the '=' operator but is there anybody writing things like in real-life code:

for (int i(0); i < 5; ++i) {
    cout << i << endl;
}

Thanks.

EDIT: The question asks about coding styles and best practices rather than technical implementation.

4
  • "Do you ever write code like this?" isn't really a suitable question for SO... Commented Aug 17, 2011 at 11:12
  • 1
    I've seen code like this and I don't like it. I even initialize class-type variables using the copy initialization syntax if possible. Copy elisions FTW. Commented Aug 17, 2011 at 11:19
  • 1
    @Oli Charlesworth I am asking for best practices and recommended styles. Commented Aug 17, 2011 at 11:29
  • Subjective and broad questions like this are not for Stack Overflow. Commented Aug 17, 2011 at 11:33

4 Answers 4

3

Both are initialization using the copy constructor, even though the first looks like an assignment. It's just syntactic sugar.

You could check it easily with a class that prints out something at copy construction and something different at assignment.

And int being a primitive doesn't even come into play.

Sign up to request clarification or add additional context in comments.

Comments

2

Some people do this to be consistent.

Inside a template, the code could be

for (T i(0); i < 5; ++i) {
    cout << i << endl;
}

and writing it that way everywhere would make the coding style consistent.

7 Comments

Why couldn't it be written the other way around inside a template?
This saves type T from having an assignment operator taking a 0. It is enough to have a constructor.
To have a constructor taking a 0, right? So it would work if writing i = 0 anyway.
Not if the constructor is explicit. The idea is to minimize the requirements on the type T, so the code works in as many cases as possible.
In this case you require that type T behaves almost like an int, so it implements a constructor taking an int, the operator< and the prefix operator++. It's just a non-sense that type T provides all these and not the assignment operator for int. IMHO it is not worth to spread a harder to read code style through your codebase just for this reason.
|
1

I prefer the i = 0 style, it is easier to read, and you can also use it in like this:

if (int i = some_function())
{
}

Works also fine with pointers, and all other types convertible to bool:

if (const int* p = some_function())
{
}

if (shared_ptr<const int> q = some_function())
{
}

Comments

1

I used to have a colleague doing so:

for (int i(0); i < 5; ++i) {
    cout << i << endl;
}

and it really pissed everyone off. It's far easier to read the code using i = 0 than i(0). Maybe not in this example but, as a general rule, it is.

Even for complex objects, I always prefer the i = 0 style, it just feels more natural for the reader and any decent compiler will optimize the generated code so there is virtually no performance penalty.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.