Jump to content
  • Advertisement
Josheir

C++ Enum Trick Answer, Please.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Sincerely,

Josheir

Edited by Josheir

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!