# Constness questions

This topic is 404 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I have a few questions about constness in AS. I do like const both in C++ and AS because it helps with maintainability, but I was wondering how it could change performance for some cases I couldn't find on the wiki:

• Can registering methods as const methods when relevant ever help with script performance or could help crafting faster scripts?
• Can defining primitives or objects as const help with performance?

I also have a question for something I'm not sure about: With AS 2.27.1 (which we are upgrading when we get the weirdness on our VS2010 builds sorted out), I can't seem to do something like:

const SColor white(255, 255, 255, 255);

switch(0xFFFFFFFF)
{
case white.color: print('ok');
};

I do get this error:

Script: Case expressions must be constants

Is this a bug or is this expected? Of course, our usecase is not as dumb as this example..

Thanks for your work on AS

Edited by Asu

##### Share on other sites

Methods being const or non-const doesn't make a difference in terms of performance. If you have a const reference to an object, then you'll only be able to call const methods on that object.

Declaring primtives and objects as const can help improve performance. The compiler is able to evaluate at compile time some operations involving constant primitives. Also when declaring function arguments, prefer the 'const type &in' variant as in some cases the compiler will be able to avoid making a copy of the value if it can determine that the original value has a guaranteed lifetime.

As for the question about the switch statement. At this moment the switch cases must use literal constants that can be evaluated at compile time. It is not enough to be read-only objects, because these are not evaluated at compile time.

On the VS2010 problem. Is there a specific reason why you're still using VS2010? Have you tried a newer version of VS? I believe VS2017 is already available (though I'm still using VS2015).

##### Share on other sites

Because IIRC global variables can get initialized when/just after building, I thought the compiler would be able to figure out the case statement at compile-time... But it probably wouldn't make much sense.

We were planning upgrading compilers primarily so we don't get locked in to older C++ versions, but avoided doing both the AS upgrade and compiler upgrade at the same time for testing convenience. But due to the issue with VS2010 we might actually get VS upgraded first in case the issue goes away with a newer compiler.

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633694
• Total Posts
3013372
×