Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What other useful casts can be used in C++

Tags:

c++

casting

C++ comes with four built-in casts.

  1. static_cast
  2. dynamic_cast
  3. const_cast
  4. reinterpret_cast

Not to meantion the frowned upon C (style*)cast.

Additionally boost supplies a lexical_cast, are there any other useful casts that you use or would like to exist?

like image 233
Motti Avatar asked May 14 '09 11:05

Motti


People also ask

What are different types of type casting in C?

In-built type casting functions in C:-atof(): Used for converting the string data type into float data type. atoi(): Used for converting the string data type into int data type. atbol(): Used for converting the string data type into long data type. itoba(): Used for converting the int data type into string data type.

Does C support type casting?

Typecasting is a method in C language of converting one data type to another. There are two types of typecasting. 1. Implicit Type casting − This conversion is done by the compiler.

What are cast operators in C?

The cast operator is a unary type operator that temporarily transforms an expression, variable, or constant to a specific data type. Syntax: (data-type)expression; Here the data type is the type in which we are converting the expression, where the expression could be either a variable, constant, or an expression.

What type casting is required in programming?

Understanding Type Casting in Java Programmers need to check the compatibility of the data type they are assigning to another data type, in advance. Typecasting is automatically performed if compatibility between the two data types exists. This type of conversion is called automatic type conversion.


2 Answers

My favorite and most loved cast is implicit_cast. It only succeeds if the types can be implicitly converted.

Useful for conversion from some type into void* or from some derived class into a base (if you want to select a specific instance of an overloaded function or constructor) or to safely add const-qualifications and any other scenario where you really just need implicit conversions to happen and even static_cast is too powerful.

Also read How does C++ pick which overload to call.

boost/implicit_cast.hpp. You can add this to your code collection too, if you want

template<typename T> struct identity { typedef T type; };
template<typename Dst> Dst implicit_cast(typename identity<Dst>::type t)
{ return t; }
like image 65
Johannes Schaub - litb Avatar answered Sep 24 '22 18:09

Johannes Schaub - litb


There's also the function-style cast which looks like a function or constructor call. This resolves to the constructor call for classes, and (more generally) to C-style casts for all other types.

Examples:

int x = int(1.0);       // equals `(int)1.0`
string s = string("x"); // equals call to constructor

The call to a constructor can also be achived using an explicit cast (apart from the C-style cast which would also work):

string s = static_cast<string>("x"); // is the same as above!
like image 26
Konrad Rudolph Avatar answered Sep 24 '22 18:09

Konrad Rudolph