Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 152 Vote(s) - 3.59 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ variable declation syntax

#1
I recently came across this construct:
int(m);
which seems to be equivalent to:
int m;

Oddly, I have never seen this particular idiom before. Can someone point me to a reference where I can read the spec on this, or just explain directly? Does this also work in straight C?

Thanks,
ConfusedDeveloper
Reply

#2
It is not an "idiom". It is just a redundant pair of parentheses. Grammatically, they can be there, but they serve no purpose.

Sometimes similar seemingly superfluous parentheses can be used to resolve ambiguity in C++ declarations, like

int a(int());

which declares a function can be turned into


int a((int()));

which is equivalent to

int a = int();

and defines a variable. But this is not exactly what you have in your case.
Reply

#3
It's also used to cast. Like,

double m= 10.0;
int n= int(m);
Reply

#4
In addition to other answers, sometimes declarator has to be parenthesized.<br>
For example:

struct A {};

struct B { A a; };

namespace N {
struct B { int a; };

void f()
{
A (::B::*p) = &::B::a; // this () cannot be omitted
}
}

If `()` is omitted in the above code, compiler recognizes a consecutive nested
name specifier `A::B` instead of `A` and `::B`, and will issue an error.<br>
This parenthesis is necessary, but sometimes leads to a misleading situation.

struct A {
int m;
A() {}
A( int ) {} // this isn't called
};

int i;

int main()
{
A(i); // this is a declaration
i.m = 1; // ok
}

In the above code, `A(i)` is a declaration(and also a definition in this case) of an object `i`,
instead of a constructor call expression with an `int` argument `i`.

Hope this helps.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through