Advertisement

C++ Enum Trick Answer, Please.

Started by October 30, 2017 08:00 PM
6 comments, last by Josheir 7 years ago

Hello everyone we're approaching the holiday season! :)

I am looking at the following specializing for C++ type information:

 

struct Yes {};

struct No {};

.

.

template <typename T>

struct IsPtr<T *>  // unqualified ptr.

{ enum {result = true}; typedef Yes Result};

.

.

.

The result is not the same as the different cased Result  of the typedef?

 

My other (more important) question is what is going on with the enum :      enum {result = true};

I don't know this little trick, and the result = true is either that or false for each of the similar structs.

 

Thank you; sincerely;

Josheir

28 minutes ago, Josheir said:

My other (more important) question is what is going on with the enum

An enum can be assigned a value.  result = true sets the value to 1, result = false sets the value to zero.

They use an enum because the rules allow using the value directly rather than placing them in a variable or anywhere in memory.

And they use the typedef so you don't need to actually run the code, you can look up the value as a constant expression.

The end result is that the template code optimizes away at compile time to the integer constant 1 or 0. This can be applied to other optimizations, such this type of things is usually in an if() statement. The compiler can use the constant expression to potentially eliminate even more code at compile time.

Advertisement
17 hours ago, frob said:

 

And they use the typedef so you don't need to actually run the code, you can look up the value as a constant expression.

 

What is meant by these two points, please?  I don't need to run the code and I can look up the value as a constant expression?

These little detail are important to me;

Josheir

6 minutes ago, Josheir said:

What is meant by these two points, please?  I don't need to run the code and I can look up the value as a constant expression?

The code is not run at run-time, it's result is determined at compile time and therefor constant.

Hmm... the program is still built and executed for me to look at the values though?

Sincerely,

Josheir

Yes. It's like when you write the following programming:


int main()
{
  printf("%d\n", 1 + 2);
}

It'll print out 3, but it will not calculate the result of 1 + 2 every time you run the program. Instead your compiler sees that these are constant values and can be optimized away, so it simply replaces the 1 + 2 with 3. Your program behaves like you would expect it to, but it's doing less work to get there.

The same thing happens with IsPtr, since the compiler knows what the types are, it can deduce during compile time what the result is and just bake it into your program.

Advertisement

That  is what I needed to know, now this casualness makes sense to me:

On 10/30/2017 at 5:19 PM, frob said:

 

And they use the typedef so you don't need to actually run the code, you can look up the value as a constant expression.

 

 

It's like compile-time enhancements.

 

I know this is a basic idea but I wasn't really understanding; thank you!

 

Josheir

This topic is closed to new replies.

Advertisement