auto convert? auto casting? On implicit casting in the CPP.

Asked 2 years ago, Updated 2 years ago, 73 views

Constant *putsFunc = module->getOrInsertFunction("puts", putsType);

There's a code like this. If I write it like this, the return type on the right is automatically casted as the return type on the left, right?

I remember that language C doesn't usually provide such an implicit. If you want to cast, make sure to

int a = 8.23f;

int b = (int)8.24f;

I understand that you can't do a, but you have to do b.

Does Cpp provide implicit type casting?

If there is a well-organized document, can I know the link?

c++ casting

2022-09-20 22:07

1 Answers

C++'s Implicit Conversion Rule is a little bit complex, but it is similar to most object-oriented languages and extends them further.

Supports implicit transformations for base types, such as C language. Conversion to modifiers such as const, volatile, and const volatile is also similar. In addition, upcasting is possible through inheritance relationships between classes. Inter-pointer conversion is not implicit unless upcasting.

So far, there's not much complexity about the transformation, but there's a lot of freedom, but there's a lot of complexity because it supports custom transformation.

For example, you can create a transform operator, such as operator int() in the class, as shown below.

struct Integer {
    std::string literal;
    operator int() const { return std::stoi(liternal); }
};
Integer raw = {"1234"};
int value = raw;

Let's move on to the main point and explain the code below.

Constant *putsFunc = module->getOrInsertFunction("puts", putsType);

You said that the return type of getOrInsertFunction() was converted to Constant*, but you didn't explain the detailed type, so I looked it up.

FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T);

The code appears to be written in LLVM, and the relevant class description shows that getOrInsertFunction() returns FunctionCallee. FunctionCallee's inheritance relationship or user conversion is not related to Constant, so Transformation is not possible.

However, I looked up the LLVM version because I thought you gave me a question because the code was possible and I thought it could be searched. The code seems to be roughly the code used in LLVM 3, and it is difficult to find the relevant document, so I checked the source code.

If you look here , the function is declared as follows:

Constant *getOrInsertFunction(StringRef Name, FunctionType *T);

In other words, getorinsertfunction The () * constant returns because your code is normal operation, the questions. In the same variable in the same type and returns the type of college not much is achieved by it.


2022-09-20 22:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.