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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-06-2024, 04:41 PM
|
#1
|
LQ Newbie
Registered: Jan 2024
Distribution: Debian Linux / Red Hat Enterprise
Posts: 20
Rep:
|
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.
|
|
|
07-07-2024, 11:49 AM
|
#2
|
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,295
|
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.
|
|
|
07-07-2024, 12:01 PM
|
#3
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,924
|
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.
|
|
|
07-07-2024, 03:21 PM
|
#4
|
Senior Member
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,201
|
How can it be a constexpr if you are calling a function with a variable argument?
|
|
|
07-07-2024, 05:13 PM
|
#5
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,827
|
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.
|
|
|
07-09-2024, 07:53 AM
|
#6
|
Member
Registered: Aug 2020
Posts: 90
Rep:
|
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.
|
|
|
07-09-2024, 08:23 AM
|
#7
|
Senior Member
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,015
|
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
|
|
|
07-09-2024, 09:45 AM
|
#8
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,827
|
Of course, I was not wondering about consts, but specifically: constexpr ...
|
|
|
All times are GMT -5. The time now is 05:10 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|