I have compiled and run the following code snippets in GCC
compiler. It's successfully worked.
#include <iostream>
using namespace std;
int main()
{
char ch = char('A');
cout<<ch<<endl;
return 0;
}
Output:
A
So, Is char() build-in type function or system call?
Technically, this is a type cast. The phrase used in the standard to describe the syntax is "Explicit type conversion (functional notation)". The description of the effect from the standard ([expr.type.conv]/2) is as follows:
If the initializer is a parenthesized single expression, the type conversion expression is equivalent (in definedness, and if defined in meaning) to the corresponding cast expression (8.4). [...] [T]he expression is a prvalue of the specified type whose result object is direct-initialized (11.6) with the initializer.
In this case, you're starting with 'A' (a character literal), which already has type char
1, and casting it to char
(so the cast has no effect and accomplishes nothing).
If you did this for a type that had a constructor that took the correct argument type, the constructor for that type could be used to do the conversion. But that's also the case for other cast notations, such as using (T)x
or static_cast<T>(x)
. There's nothing about this syntax that makes it any more about using a ctor than any other other syntax that can do a conversion. Since you have made the conversion explicit, it can be used to invoke a ctor that's marked explicit
, but other than that, it's not really any different (in terms of whether it uses a ctor or not) than just T ch = value;
(without a cast, but possibly including a conversion anyway).
1. Note that this is one place that C++ differs from C. In C, a character literal has type int
, not char
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With