Thread: Helping me to understand a little conc ept about variables and constexpr

  1. #1
    Registered User
    Join Date
    Nov 2014
    Location
    Naples Italy
    Posts
    3

    Helping me to understand a little conc ept about variables and constexpr

    Hello everyone, I started about a month to learn C ++ through the book: Programming Principles and Practice using C ++. My problem lies in the notion that my book explains at the beginning of the fourth chapter that I can not understand. The book introduces the notation constexpr and says in some cases the language requires the use of "constant expressions" ( expressions with an integer composed only by constants), for example :
    Code:
    constexpr int max = 10; //a literal is a constant //expression
    max + 2; // another constant expression
    A variable that uses the notation constexpr must be initialized with a value known at compile time (??). That's all I can not understand what a compile-time constant is. How does the compiler create a variable in memory that do not use the notation constexpr as:
    Code:
    int a = 10;
    and what's the difference between this variable and a variable that uses contexpr ?. I can not understand what other people intend to say that if a variable uses constexpr then its value is already known at compile time, isnt' the same for the variable a in my example ? I mean that I initialize that variable with a constant expression and I will use its value for the rest of the program, so why it is not a value known at compile time ?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are right that the value of the variable "a" in your example in known at compile time. Indeed, "10" is a constant expression (otherwise const int a = 10 or constexpr int a = 10 wouldn't compile).
    However, your variable "a" is not a constant expression as the language sees it. Yes, the compiler can probably optimize your code such that when you reference "a" in your code, it can substitute that with 10. But for things in the language that require a compile-time expression, it will not do. That's the way the language works.
    So in order to tell the compiler that this is indeed a compile-time expression (i.e. an expression that the compiler knows when compiling the code), then you should mark the variable with "constexpr." Btw, constexpr works for functions too, allowing you to run a function at compile time. Consider:

    Code:
    constexpr int max(int a, int b) { return (a >= b ? a : b); }
    constexpr int m = max(5, 10);
    The function is run by the compiler when compiling the code and the result 10 will be stored in your constant expression variable m.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Constexpr and generalized constant expressions in C++11
    By webmaster in forum C++ Programming
    Replies: 8
    Last Post: 09-29-2011, 06:37 PM
  2. Helping With Warnings
    By c9dw2rm8 in forum C Programming
    Replies: 8
    Last Post: 02-14-2008, 09:55 AM
  3. Helping out a friend...
    By rhymewithnothin in forum C Programming
    Replies: 21
    Last Post: 09-15-2005, 08:59 PM
  4. Helping with an error i keep getting
    By FlyingKoopaint in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2005, 05:50 PM
  5. interested in helping????
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 05-05-2002, 09:02 PM

Tags for this Thread