What is the difference between a (type) value and a type (value)?

What's the difference between

(type)value 

and

 type(value) 

in c ++?

+41
c ++ casting type-conversion
Oct 30 '09 at 21:23
source share
5 answers

There is no difference; according to the standard (clause 5.2.3):

The simple type specifier (7.1.5), followed by a parenthesized list of expressions, builds the value of the specified type based on the list of expressions. If the list of expressions is a single expression, the type conversion expression is equivalent (in certainty and, if defined in meaning) to the corresponding expression (5.4).

Since the question asks the difference between type(value) and (type)value , there is no difference.

If and only if you are dealing with a comma-separated list of values, there might be a difference. In this case:

If more than one value is specified in the list of expressions, the type must be a class with the corresponding declared constructor (8.5, 12.1), and the expression T (x1, x2, ...) is actually equivalent to the declaration T t (x1, x2, ...); for some invented temporary variable t, the result of which is the value of t as rvalue.

As Troubadour pointed out, there are certain type names for which the version of type(value) simply will not compile. For example:

 char *a = (char *)string; 

will compile but:

 char *a = char *(string); 

will not be. The same type with a different name (for example, created using typedef ) may work:

 typedef char *char_ptr; char *a = char_ptr(string); 
+50
Oct 30 '09 at 21:25
source share

There is no difference; the C ++ standard (1998 and 2003 releases) is clear from this point. Try the following program, make sure you are using a compatible compiler, for example, a free preview at http://comeaucomputing.com/tryitout/ .

 #include <cstdlib> #include <string> int main() { int('A'); (int) 'A'; // obvious (std::string) "abc"; // not so obvious unsigned(a_var) = 3; // see note below (long const&) a_var; // const or refs, which T(v) can't do return EXIT_SUCCESS; } 

Note: unsigned(a_var) is different, but shows one way that exact tokens can mean something else. It declares a variable named a_var type unsigned and is not cast. (If you are familiar with pointers to functions or arrays, consider how you should use parens around p in a type like void (*pf)() or int (*pa)[42] .)

(Warnings are issued because these statements do not use the meaning in a real program, which will almost certainly be a mistake, but it still works. I just didn't have the heart to change it after creating the entire line before.)

+13
Oct 30 '09 at 21:55
source share

It makes no difference if both are casts, but sometimes "type (value)" is not cast.

Here is an example of a standard draft of N3242, section 8.2.1:

 struct S { S(int); }; void foo(double a) { S w( int(a) ); // function declaration S y( (int)a ); // object declaration } 

In this case, 'int (a)' is not an act, because 'a' is not a value, it is the name of the parameter surrounded by backup brackets. The document states

The ambiguity arising from the similarities between the functional style and the application mentioned in 6.8 may also arise in the context of the declaration. In this context, the choice between the declaration function with an excessive set of parentheses around the name parameter and the declaration of the object using the style function as an initializer. As well as for the ambiguities mentioned in 6.8, the permission is to consider any construct that might be to declare a declaration.

+5
Dec 11 2018-11-11T00:
source share

In c there is no type (value) , while in c / C ++ both type (value) and (type) value allowed.

+1
Feb 10 '13 at 22:27
source share

To illustrate your options in C ++ (only one has a security check)

 #include<boost/numeric/conversion/cast.hpp> using std::cout; using std::endl; int main(){ float smallf = 100.1; cout << (int)smallf << endl; // outputs 100 // c cast cout << int(smallf) << endl; // outputs 100 // c++ constructor = c cast cout << static_cast<int>(smallf) << endl; // outputs 100 // cout << static_cast<int&>(smallf) << endl; // not allowed cout << reinterpret_cast<int&>(smallf) << endl; // outputs 1120416563 cout << boost::numeric_cast<int>(smallf) << endl; // outputs 100 float bigf = 1.23e12; cout << (int)bigf << endl; // outputs -2147483648 cout << int(bigf) << endl; // outputs -2147483648 cout << static_cast<int>(bigf) << endl; // outputs -2147483648 // cout << static_cast<int&>(bigf) << endl; // not allowed cout << reinterpret_cast<int&>(bigf) << endl; // outputs 1401893083 cout << boost::numeric_cast<int>(bigf) << endl; // throws bad numeric conversion } 
0
Jul 08 '15 at 3:50
source share



All Articles