LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-06-2024, 04:41 PM   #1
Mac1ek
LQ Newbie
 
Registered: Jan 2024
Distribution: Debian Linux / Red Hat Enterprise
Posts: 20

Rep: Reputation: 1
c++ - constexpr


I have question - if i have loop - for, when in compilation time index begin to end is knowed, i cant use constexpr for this evaluation like this:
Code:
for(int i = 0; i<10; ++i) {
   constexpr foo(i);
}
? Thanks.

Last edited by Mac1ek; 07-06-2024 at 04:50 PM.
 
Old 07-07-2024, 11:49 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,295
Blog Entries: 24

Rep: Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254
As no one else has stepped up...

I have never actually used c++ constexpr but from looking over the docs and an article here, I think you cannot use it as in your example.

First, foo(int) would have to have been declared as constexpr which is not shown in your example. but even if it were it's parameters must be known at compile time and in your example that is not possible. Even though you know the range of i within the loop, it is still a range, not a constant value that can be known at compile time.
 
Old 07-07-2024, 12:01 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,924
Blog Entries: 1

Rep: Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886
I guess it is not impossible to actually try it. In my case it didn't work
Code:
/* Mac1ek.cc */

extern int foo(int);

int main ()
{
    for(int i = 0; i<10; ++i) {
        constexpr foo(i);
    }
    return 0;
}
compilation:
Code:
g++ -std=c++23 -o Mac1ek Mac1ek.cc Mac1ek2.cc
Mac1ek.cc: In function ‘int main()’:
Mac1ek.cc:8:19: error: ISO C++ forbids declaration of ‘foo’ with no type [-fpermissive]
    8 |         constexpr foo(i);
      |                   ^~~
Mac1ek.cc:8:23: error: the value of ‘i’ is not usable in a constant expression
    8 |         constexpr foo(i);
      |                       ^
Mac1ek.cc:7:13: note: ‘int i’ is not const
    7 |     for(int i = 0; i<10; ++i) {
      |             ^

Last edited by NevemTeve; 07-07-2024 at 12:04 PM.
 
Old 07-07-2024, 03:21 PM   #4
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,201

Rep: Reputation: 1284Reputation: 1284Reputation: 1284Reputation: 1284Reputation: 1284Reputation: 1284Reputation: 1284Reputation: 1284Reputation: 1284
How can it be a constexpr if you are calling a function with a variable argument?
 
Old 07-07-2024, 05:13 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,827
Blog Entries: 4

Rep: Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984
To me, constexpr is yet-another "over-engineered thing" that someone added to C++. The formal description says:

Quote:
The constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time. Such variables and functions can then be used where only compile time constant expressions are allowed [...]
So it functions like a "constant," because it can be evaluated at compile time, but it is an "expression." The compiler now has to do all the heavy-lifting to figure out what the "constant value" is, in any particular case. But, having done so, the result is "a constant value."

Clearly, in your case this cannot be done: you cannot possibly "evaluate foo(i) at compile time," simply because of "i"!

Incidentally, I'd be interested to hear from any C++ programmers out there who think that this "fee-chur" is "the bee's knees ..." If a use-case exists to justify this level of compiler complexity, I'd like to hear what it is.
 
Old 07-09-2024, 07:53 AM   #6
slackerz
Member
 
Registered: Aug 2020
Posts: 90

Rep: Reputation: 31
Ideally what you want to do is constexpr pretty much everything in your code. In practice the use of constexpr ultimately reduces to a handful of constants in the program.
 
Old 07-09-2024, 08:23 AM   #7
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,015

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
Gcc is very good at inlining functions and evaluating constant expressions at compile time. Here is an example:

Code:
// compile with g++ -O3 -Wall -S test.cpp

namespace {
  long i=4;
  long foo () { return i; }
}

long bar () { return foo (); }
The assembly code for bar () simply returns 4. No code is generated for i or foo ().

I use const but have not found a need for constexpr.
Ed
 
Old 07-09-2024, 09:45 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,827
Blog Entries: 4

Rep: Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984Reputation: 3984
Of course, I was not wondering about consts, but specifically: constexpr ...
 
  


Reply

Tags
c++


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration