What exactly is a const expression?

This is a discussion on What exactly is a const expression? within the C++ Programming forums, part of the General Programming Boards category; I'm reading about classes and header files in my book right now. It's talking about const definitions in header files ...

  1. #1
    gin
    gin is offline
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51

    What exactly is a const expression?

    I'm reading about classes and header files in my book right now. It's talking about const definitions in header files saying that you can only define a const with a const expression. What exactly is a const expression? I thought an expression was something that yields a result and/or side effect. Then it says if the const doesn't have a const expression it shouldn't be defined in a header file, instead it should be defined in a source file and then declared in the header file. How can this be if the value you initialize to a const has to be known at compile-time? Is this meaning literals?

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    const modifier means its final e.g. Its value cant be changed letter.
    e.g.
    const int x=2;
    x=3;//Wrong

  3. #3
    gin
    gin is offline
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    I know what a const is, I was asking what is a const expression. Because it says you should only define and initialize a const in a header file with a const expression, and nothing else. But does an expression not get calculated at run-time?

    I appreciate your help though.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    I dont think const gets resolved at runtime it gets resolved at compile time.
    can you send any more texts from your book ??

  5. #5
    gin
    gin is offline
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    In C++ there are places where constant expression (Section 2.7, p. 62) is required. For example, the initializer of an enumerator must be a constant expression. We'll see other cases that require constant expressions in later chapters.

    Generally speaking, a constant expression is an expression that the compiler can evaluate at compile-time. A const variable of integral type may be a constant expression when it is itself initialized from a constant expression. However, for the const to be a constant expression, the initializer must be visible to the compiler. To allow multiple files to use the same constant value, the const and its initializer must be visible in each file. To make the initializer visible, we normally define such consts inside a header file. That way the compiler can see the initializer whenever the const is used.
    I just gave it a re-read there and I saw that the expression is evaluated at compile-time. But what is a const expression? Is this a const expression?:

    Code:
    const int a = 5;
    const int b = 4;
    
    const int result = a + b; //Const expression?

  6. #6
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Quote Originally Posted by gin View Post
    Code:
    const int a = 5;
    const int b = 4;
    
    const int result = a + b; //Const expression?
    In this case, a + b is a const expression. But this wouldn't be a const expresion:

    Code:
    const int a = 5;
    int b = 4;
    
    a + b;  // Non const expression
    I hate real numbers.

  7. #7
    gin
    gin is offline
    Hmm...? gin's Avatar
    Join Date
    Jun 2008
    Location
    Glasgow, Scotland
    Posts
    51
    Ah I see. That answers the first part. Why are you not allowed to define and intialize a const in a header file with a literal (a literal isn't a const expression?).

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Its true that the author makes 2+2 = ((22+88)/50)*2 = 4
    I think what he meant to say by the word "expression" is the variables/identifiers like a, b, result
    and by the word "initializer" he/she meant the value stored in a const identifier.
    (as that value can not be changed he said initializer).
    and as I've already said Its resolved at compile Time.

  9. #9

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    Sometimes a const variable can be initialized dynamically (google 'dynamic initialization') and it will not be considered a constant expression. Constant expressions are required when defining arrays, specifying template parameters, etc.

    For example,
    Code:
    const int add_numbers (const int a, const int b)
    {
       return a + b;
    }
    
    const int FOUR = add_numbers (3, 1);
    'FOUR' would not be considered a constant expression, because its initializer is not one. Some compilers may or may not be more lenient with this, but some people have proposed adding a new keyword, constexpr, which is likely to be implemented in the next C++ standard.

  10. #10
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Quote Originally Posted by rudyman View Post
    Sometimes a const variable can be initialized dynamically (google 'dynamic initialization') and it will not be considered a constant expression. Constant expressions are required when defining arrays, specifying template parameters, etc.

    For example,
    Code:
    const int add_numbers (const int a, const int b)
    {
       return a + b;
    }
    
    const int FOUR = add_numbers (3, 1);
    'FOUR' would not be considered a constant expression, because its initializer is not one. Some compilers may or may not be more lenient with this, but some people have proposed adding a new keyword, constexpr, which is likely to be implemented in the next C++ standard.
    are you sure that its not considered as constant expression its done at runtime. ??
    But I think its still a constant expression cause add_numbers returns constant integer.

  11. #11

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    It returns a const int, but is not considered a constant expression. In C, which doesn't support dynamic initialization, it would be illegal to initialize a global variable like that, or to use that function to declare an array, etc.

    C++ supports dynamic initialization, but it still follows the same constant expression rules. Try saying, for example:

    Code:
    template <int Size>
    class Test
    {
       /* ... */
    };
    
    Test <add_numbers (3, 1)> obj; // Illegal. Template param is not a const expr.
    
    int Four_Integers [add_numbers (3, 1)]; /* EDIT: Yes, this is a variable length array,
                                            because it's not declared with a const expr. */
    Last edited by rudyman; 07-14-2008 at 12:49 PM.

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That array declaration should only be legal in C99, since it is still considered to be a variable-sized array. (Some C++ compilers support this as an extension.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Anon is correct. A function return is NOT considered a constant expression (although the next standard will add a feature that allows a return from a function to be interpreted as a constant expression).
    This is often circumvented via static constant variables inside structs through template programming.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    I might be wrong.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    A constant expression is evaluated at compile time.
    If it cannot be evaluated at compile time, it is not a constant expression.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21