C++ check if statement can be evaluated constexprWhat are the differences between a pointer variable and a reference variable in C++?How can I profile C++ code running on Linux?The Definitive C++ Book Guide and ListWhy can templates only be implemented in the header file?What is the effect of extern “C” in C++?What is the “-->” operator in C++?Easiest way to convert int to string in C++C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why is reading lines from stdin much slower in C++ than Python?Difference between `constexpr` and `const`
Simulating a probability of 1 of 2^N with less than N random bits
Is it okay / does it make sense for another player to join a running game of Munchkin?
What linear sensor for a keyboard?
QGIS Geometry Generator Line Type
What should I use for Mishna study?
Suggestion on Missing Index Creation
Adding empty element to declared container without declaring type of element
Freedom of speech and where it applies
The most efficient algorithm to find all possible integer pairs which sum to a given integer
How to deal with loss of decision making power over a change?
Is there a problem with hiding "forgot password" until it's needed?
What does the "3am" section means in manpages?
I2C signal and power over long range (10meter cable)
Teaching indefinite integrals
Can I Retrieve Email Addresses from BCC?
Hostile work environment after whistle-blowing on coworker and our boss. What do I do?
How to decode Core DB Account.Signers using the stellar js-sdk
Have I saved too much for retirement so far?
How did Monica know how to operate Carol's "designer"?
Do these cracks on my tires look bad?
In Star Trek IV, why did the Bounty go back to a time when whales were already rare?
Is it possible to build a CPA Secure encryption scheme which remains secure even when the encryption of secret key is given?
Partial sums of primes
Reply ‘no position’ while the job posting is still there (‘HiWi’ position in Germany)
C++ check if statement can be evaluated constexpr
What are the differences between a pointer variable and a reference variable in C++?How can I profile C++ code running on Linux?The Definitive C++ Book Guide and ListWhy can templates only be implemented in the header file?What is the effect of extern “C” in C++?What is the “-->” operator in C++?Easiest way to convert int to string in C++C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why is reading lines from stdin much slower in C++ than Python?Difference between `constexpr` and `const`
Is there a method to decide whether something can be constexpr evaluated, and use the result as a constexpr boolean? My simplified use case is as follows:
template <typename base>
class derived
template<size_t size>
void do_stuff() (...)
void do_stuff(size_t size) (...)
public:
void execute()
if constexpr(is_constexpr(base::get_data())
do_stuff<base::get_data()>();
else
do_stuff(base::get_data());
My target is C++2a.
I found the following reddit thread, but I'm not a big fan of the macros. https://www.reddit.com/r/cpp/comments/7c208c/is_constexpr_a_macro_that_check_if_an_expression/
c++ template-meta-programming constexpr c++20 if-constexpr
|
show 3 more comments
Is there a method to decide whether something can be constexpr evaluated, and use the result as a constexpr boolean? My simplified use case is as follows:
template <typename base>
class derived
template<size_t size>
void do_stuff() (...)
void do_stuff(size_t size) (...)
public:
void execute()
if constexpr(is_constexpr(base::get_data())
do_stuff<base::get_data()>();
else
do_stuff(base::get_data());
My target is C++2a.
I found the following reddit thread, but I'm not a big fan of the macros. https://www.reddit.com/r/cpp/comments/7c208c/is_constexpr_a_macro_that_check_if_an_expression/
c++ template-meta-programming constexpr c++20 if-constexpr
1
Hmm, the body of aif constexpr
will only be evaluated if the expression in theif constexpr
is true at compile time. Is that what you are looking for?
– Jesper Juhl
Mar 21 at 20:13
1
But what if the test in the if constexpr([test]) is not evaluatable at compile time?
– Aart Stuurman
Mar 21 at 20:17
6
Maybe you can do something withstd::is_constant_evaluated
?
– 0x5453
Mar 21 at 20:18
en.cppreference.com/w/cpp/language/if
– Jesper Juhl
Mar 21 at 20:18
2
@AartStuurman: What isdo_stuff
that it can run at compile time or runtime, but itself should not beconstexpr
? Wouldn't it make more sense to just make it aconstexpr
function, and pass it the value ofget_data
as a parameter?
– Nicol Bolas
Mar 21 at 20:38
|
show 3 more comments
Is there a method to decide whether something can be constexpr evaluated, and use the result as a constexpr boolean? My simplified use case is as follows:
template <typename base>
class derived
template<size_t size>
void do_stuff() (...)
void do_stuff(size_t size) (...)
public:
void execute()
if constexpr(is_constexpr(base::get_data())
do_stuff<base::get_data()>();
else
do_stuff(base::get_data());
My target is C++2a.
I found the following reddit thread, but I'm not a big fan of the macros. https://www.reddit.com/r/cpp/comments/7c208c/is_constexpr_a_macro_that_check_if_an_expression/
c++ template-meta-programming constexpr c++20 if-constexpr
Is there a method to decide whether something can be constexpr evaluated, and use the result as a constexpr boolean? My simplified use case is as follows:
template <typename base>
class derived
template<size_t size>
void do_stuff() (...)
void do_stuff(size_t size) (...)
public:
void execute()
if constexpr(is_constexpr(base::get_data())
do_stuff<base::get_data()>();
else
do_stuff(base::get_data());
My target is C++2a.
I found the following reddit thread, but I'm not a big fan of the macros. https://www.reddit.com/r/cpp/comments/7c208c/is_constexpr_a_macro_that_check_if_an_expression/
c++ template-meta-programming constexpr c++20 if-constexpr
c++ template-meta-programming constexpr c++20 if-constexpr
edited Mar 22 at 14:35
max66
38.3k74473
38.3k74473
asked Mar 21 at 20:10
Aart StuurmanAart Stuurman
1,006827
1,006827
1
Hmm, the body of aif constexpr
will only be evaluated if the expression in theif constexpr
is true at compile time. Is that what you are looking for?
– Jesper Juhl
Mar 21 at 20:13
1
But what if the test in the if constexpr([test]) is not evaluatable at compile time?
– Aart Stuurman
Mar 21 at 20:17
6
Maybe you can do something withstd::is_constant_evaluated
?
– 0x5453
Mar 21 at 20:18
en.cppreference.com/w/cpp/language/if
– Jesper Juhl
Mar 21 at 20:18
2
@AartStuurman: What isdo_stuff
that it can run at compile time or runtime, but itself should not beconstexpr
? Wouldn't it make more sense to just make it aconstexpr
function, and pass it the value ofget_data
as a parameter?
– Nicol Bolas
Mar 21 at 20:38
|
show 3 more comments
1
Hmm, the body of aif constexpr
will only be evaluated if the expression in theif constexpr
is true at compile time. Is that what you are looking for?
– Jesper Juhl
Mar 21 at 20:13
1
But what if the test in the if constexpr([test]) is not evaluatable at compile time?
– Aart Stuurman
Mar 21 at 20:17
6
Maybe you can do something withstd::is_constant_evaluated
?
– 0x5453
Mar 21 at 20:18
en.cppreference.com/w/cpp/language/if
– Jesper Juhl
Mar 21 at 20:18
2
@AartStuurman: What isdo_stuff
that it can run at compile time or runtime, but itself should not beconstexpr
? Wouldn't it make more sense to just make it aconstexpr
function, and pass it the value ofget_data
as a parameter?
– Nicol Bolas
Mar 21 at 20:38
1
1
Hmm, the body of a
if constexpr
will only be evaluated if the expression in the if constexpr
is true at compile time. Is that what you are looking for?– Jesper Juhl
Mar 21 at 20:13
Hmm, the body of a
if constexpr
will only be evaluated if the expression in the if constexpr
is true at compile time. Is that what you are looking for?– Jesper Juhl
Mar 21 at 20:13
1
1
But what if the test in the if constexpr([test]) is not evaluatable at compile time?
– Aart Stuurman
Mar 21 at 20:17
But what if the test in the if constexpr([test]) is not evaluatable at compile time?
– Aart Stuurman
Mar 21 at 20:17
6
6
Maybe you can do something with
std::is_constant_evaluated
?– 0x5453
Mar 21 at 20:18
Maybe you can do something with
std::is_constant_evaluated
?– 0x5453
Mar 21 at 20:18
en.cppreference.com/w/cpp/language/if
– Jesper Juhl
Mar 21 at 20:18
en.cppreference.com/w/cpp/language/if
– Jesper Juhl
Mar 21 at 20:18
2
2
@AartStuurman: What is
do_stuff
that it can run at compile time or runtime, but itself should not be constexpr
? Wouldn't it make more sense to just make it a constexpr
function, and pass it the value of get_data
as a parameter?– Nicol Bolas
Mar 21 at 20:38
@AartStuurman: What is
do_stuff
that it can run at compile time or runtime, but itself should not be constexpr
? Wouldn't it make more sense to just make it a constexpr
function, and pass it the value of get_data
as a parameter?– Nicol Bolas
Mar 21 at 20:38
|
show 3 more comments
3 Answers
3
active
oldest
votes
Here's another solution, which is more generic (applicable to any expression, without defining a separate template each time).
This solution leverages that (1) lambda expressions can be constexpr as of C++17 (2) the type of a captureless lambda is default constructible as of C++20.
The idea is, the overload that returns true
is selected when and only when Lambda()
can appear within a template argument, which effectively requires the lambda invocation to be a constant expression.
template<class Lambda, int=(Lambda(), 0)>
constexpr bool is_constexpr(Lambda) return true;
constexpr bool is_constexpr(...) return false;
template <typename base>
class derived
// ...
void execute()
if constexpr(is_constexpr([] base::get_data(); ))
do_stuff<base::get_data()>();
else
do_stuff(base::get_data());
Intriguing solution... this way you get the same result of my custom type traits but more synthetically and, above all, the exact expression verified (base::get_data()
) is embedded in the argument and not hard-coded as in my solution. Very nice. I have to remember it.
– max66
Mar 22 at 0:24
I am accepting this, because it is an answer to the a generic case of the question. max66 answer is also very useful(in non-c++2a cases), but requires repetition for every usage :)
– Aart Stuurman
Mar 22 at 9:42
add a comment |
Not exactly what you asked (I've developer a custom type trait specific for a get_value()
static method... maybe it's possible to generalize it but, at the moment, I don't know how) but I suppose you can use SFINAE and make something as follows
#include <iostream>
#include <type_traits>
template <typename T>
constexpr auto icee_helper (int)
-> decltype( std::integral_constant<decltype(T::get_data()), T::get_data()>,
std::true_type );
template <typename>
constexpr auto icee_helper (long)
-> std::false_type;
template <typename T>
using isConstExprEval = decltype(icee_helper<T>(0));
template <typename base>
struct derived
template <std::size_t I>
void do_stuff()
std::cout << "constexpr case (" << I << ')' << std::endl;
void do_stuff (std::size_t i)
std::cout << "not constexpr case (" << i << ')' << std::endl;
void execute ()
if constexpr ( isConstExprEval<base>::value )
do_stuff<base::get_data()>();
else
do_stuff(base::get_data());
;
struct foo
static constexpr std::size_t get_data () return 1u; ;
struct bar
static std::size_t get_data () return 2u; ;
int main ()
derived<foo>.execute(); // print "constexpr case (1)"
derived<bar>.execute(); // print "not constexpr case (2)"
2
This is madness, this use of the comma operator, the long/int overload... Have an upvote. :/
– matovitch
Mar 21 at 21:13
@matovitch - never underestimate the power of the comma operator }:‑)
– max66
Mar 21 at 21:16
Will this work on platforms wheresizeof(long)
is equal tosizeof(int)
?
– Gregory Nisbet
Mar 22 at 3:35
2
@GregoryNisbet - Yes. Because, for the language so for the compiler, they remain different types.
– max66
Mar 22 at 8:08
add a comment |
template<auto> struct require_constant;
template<class T>
concept has_constexpr_data = requires typename require_constant<T::get_data()>; ;
This is basically what's used by std::ranges::split_view
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55288555%2fc-check-if-statement-can-be-evaluated-constexpr%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here's another solution, which is more generic (applicable to any expression, without defining a separate template each time).
This solution leverages that (1) lambda expressions can be constexpr as of C++17 (2) the type of a captureless lambda is default constructible as of C++20.
The idea is, the overload that returns true
is selected when and only when Lambda()
can appear within a template argument, which effectively requires the lambda invocation to be a constant expression.
template<class Lambda, int=(Lambda(), 0)>
constexpr bool is_constexpr(Lambda) return true;
constexpr bool is_constexpr(...) return false;
template <typename base>
class derived
// ...
void execute()
if constexpr(is_constexpr([] base::get_data(); ))
do_stuff<base::get_data()>();
else
do_stuff(base::get_data());
Intriguing solution... this way you get the same result of my custom type traits but more synthetically and, above all, the exact expression verified (base::get_data()
) is embedded in the argument and not hard-coded as in my solution. Very nice. I have to remember it.
– max66
Mar 22 at 0:24
I am accepting this, because it is an answer to the a generic case of the question. max66 answer is also very useful(in non-c++2a cases), but requires repetition for every usage :)
– Aart Stuurman
Mar 22 at 9:42
add a comment |
Here's another solution, which is more generic (applicable to any expression, without defining a separate template each time).
This solution leverages that (1) lambda expressions can be constexpr as of C++17 (2) the type of a captureless lambda is default constructible as of C++20.
The idea is, the overload that returns true
is selected when and only when Lambda()
can appear within a template argument, which effectively requires the lambda invocation to be a constant expression.
template<class Lambda, int=(Lambda(), 0)>
constexpr bool is_constexpr(Lambda) return true;
constexpr bool is_constexpr(...) return false;
template <typename base>
class derived
// ...
void execute()
if constexpr(is_constexpr([] base::get_data(); ))
do_stuff<base::get_data()>();
else
do_stuff(base::get_data());
Intriguing solution... this way you get the same result of my custom type traits but more synthetically and, above all, the exact expression verified (base::get_data()
) is embedded in the argument and not hard-coded as in my solution. Very nice. I have to remember it.
– max66
Mar 22 at 0:24
I am accepting this, because it is an answer to the a generic case of the question. max66 answer is also very useful(in non-c++2a cases), but requires repetition for every usage :)
– Aart Stuurman
Mar 22 at 9:42
add a comment |
Here's another solution, which is more generic (applicable to any expression, without defining a separate template each time).
This solution leverages that (1) lambda expressions can be constexpr as of C++17 (2) the type of a captureless lambda is default constructible as of C++20.
The idea is, the overload that returns true
is selected when and only when Lambda()
can appear within a template argument, which effectively requires the lambda invocation to be a constant expression.
template<class Lambda, int=(Lambda(), 0)>
constexpr bool is_constexpr(Lambda) return true;
constexpr bool is_constexpr(...) return false;
template <typename base>
class derived
// ...
void execute()
if constexpr(is_constexpr([] base::get_data(); ))
do_stuff<base::get_data()>();
else
do_stuff(base::get_data());
Here's another solution, which is more generic (applicable to any expression, without defining a separate template each time).
This solution leverages that (1) lambda expressions can be constexpr as of C++17 (2) the type of a captureless lambda is default constructible as of C++20.
The idea is, the overload that returns true
is selected when and only when Lambda()
can appear within a template argument, which effectively requires the lambda invocation to be a constant expression.
template<class Lambda, int=(Lambda(), 0)>
constexpr bool is_constexpr(Lambda) return true;
constexpr bool is_constexpr(...) return false;
template <typename base>
class derived
// ...
void execute()
if constexpr(is_constexpr([] base::get_data(); ))
do_stuff<base::get_data()>();
else
do_stuff(base::get_data());
edited Mar 21 at 23:26
answered Mar 21 at 22:46
cpplearnercpplearner
5,52722342
5,52722342
Intriguing solution... this way you get the same result of my custom type traits but more synthetically and, above all, the exact expression verified (base::get_data()
) is embedded in the argument and not hard-coded as in my solution. Very nice. I have to remember it.
– max66
Mar 22 at 0:24
I am accepting this, because it is an answer to the a generic case of the question. max66 answer is also very useful(in non-c++2a cases), but requires repetition for every usage :)
– Aart Stuurman
Mar 22 at 9:42
add a comment |
Intriguing solution... this way you get the same result of my custom type traits but more synthetically and, above all, the exact expression verified (base::get_data()
) is embedded in the argument and not hard-coded as in my solution. Very nice. I have to remember it.
– max66
Mar 22 at 0:24
I am accepting this, because it is an answer to the a generic case of the question. max66 answer is also very useful(in non-c++2a cases), but requires repetition for every usage :)
– Aart Stuurman
Mar 22 at 9:42
Intriguing solution... this way you get the same result of my custom type traits but more synthetically and, above all, the exact expression verified (
base::get_data()
) is embedded in the argument and not hard-coded as in my solution. Very nice. I have to remember it.– max66
Mar 22 at 0:24
Intriguing solution... this way you get the same result of my custom type traits but more synthetically and, above all, the exact expression verified (
base::get_data()
) is embedded in the argument and not hard-coded as in my solution. Very nice. I have to remember it.– max66
Mar 22 at 0:24
I am accepting this, because it is an answer to the a generic case of the question. max66 answer is also very useful(in non-c++2a cases), but requires repetition for every usage :)
– Aart Stuurman
Mar 22 at 9:42
I am accepting this, because it is an answer to the a generic case of the question. max66 answer is also very useful(in non-c++2a cases), but requires repetition for every usage :)
– Aart Stuurman
Mar 22 at 9:42
add a comment |
Not exactly what you asked (I've developer a custom type trait specific for a get_value()
static method... maybe it's possible to generalize it but, at the moment, I don't know how) but I suppose you can use SFINAE and make something as follows
#include <iostream>
#include <type_traits>
template <typename T>
constexpr auto icee_helper (int)
-> decltype( std::integral_constant<decltype(T::get_data()), T::get_data()>,
std::true_type );
template <typename>
constexpr auto icee_helper (long)
-> std::false_type;
template <typename T>
using isConstExprEval = decltype(icee_helper<T>(0));
template <typename base>
struct derived
template <std::size_t I>
void do_stuff()
std::cout << "constexpr case (" << I << ')' << std::endl;
void do_stuff (std::size_t i)
std::cout << "not constexpr case (" << i << ')' << std::endl;
void execute ()
if constexpr ( isConstExprEval<base>::value )
do_stuff<base::get_data()>();
else
do_stuff(base::get_data());
;
struct foo
static constexpr std::size_t get_data () return 1u; ;
struct bar
static std::size_t get_data () return 2u; ;
int main ()
derived<foo>.execute(); // print "constexpr case (1)"
derived<bar>.execute(); // print "not constexpr case (2)"
2
This is madness, this use of the comma operator, the long/int overload... Have an upvote. :/
– matovitch
Mar 21 at 21:13
@matovitch - never underestimate the power of the comma operator }:‑)
– max66
Mar 21 at 21:16
Will this work on platforms wheresizeof(long)
is equal tosizeof(int)
?
– Gregory Nisbet
Mar 22 at 3:35
2
@GregoryNisbet - Yes. Because, for the language so for the compiler, they remain different types.
– max66
Mar 22 at 8:08
add a comment |
Not exactly what you asked (I've developer a custom type trait specific for a get_value()
static method... maybe it's possible to generalize it but, at the moment, I don't know how) but I suppose you can use SFINAE and make something as follows
#include <iostream>
#include <type_traits>
template <typename T>
constexpr auto icee_helper (int)
-> decltype( std::integral_constant<decltype(T::get_data()), T::get_data()>,
std::true_type );
template <typename>
constexpr auto icee_helper (long)
-> std::false_type;
template <typename T>
using isConstExprEval = decltype(icee_helper<T>(0));
template <typename base>
struct derived
template <std::size_t I>
void do_stuff()
std::cout << "constexpr case (" << I << ')' << std::endl;
void do_stuff (std::size_t i)
std::cout << "not constexpr case (" << i << ')' << std::endl;
void execute ()
if constexpr ( isConstExprEval<base>::value )
do_stuff<base::get_data()>();
else
do_stuff(base::get_data());
;
struct foo
static constexpr std::size_t get_data () return 1u; ;
struct bar
static std::size_t get_data () return 2u; ;
int main ()
derived<foo>.execute(); // print "constexpr case (1)"
derived<bar>.execute(); // print "not constexpr case (2)"
2
This is madness, this use of the comma operator, the long/int overload... Have an upvote. :/
– matovitch
Mar 21 at 21:13
@matovitch - never underestimate the power of the comma operator }:‑)
– max66
Mar 21 at 21:16
Will this work on platforms wheresizeof(long)
is equal tosizeof(int)
?
– Gregory Nisbet
Mar 22 at 3:35
2
@GregoryNisbet - Yes. Because, for the language so for the compiler, they remain different types.
– max66
Mar 22 at 8:08
add a comment |
Not exactly what you asked (I've developer a custom type trait specific for a get_value()
static method... maybe it's possible to generalize it but, at the moment, I don't know how) but I suppose you can use SFINAE and make something as follows
#include <iostream>
#include <type_traits>
template <typename T>
constexpr auto icee_helper (int)
-> decltype( std::integral_constant<decltype(T::get_data()), T::get_data()>,
std::true_type );
template <typename>
constexpr auto icee_helper (long)
-> std::false_type;
template <typename T>
using isConstExprEval = decltype(icee_helper<T>(0));
template <typename base>
struct derived
template <std::size_t I>
void do_stuff()
std::cout << "constexpr case (" << I << ')' << std::endl;
void do_stuff (std::size_t i)
std::cout << "not constexpr case (" << i << ')' << std::endl;
void execute ()
if constexpr ( isConstExprEval<base>::value )
do_stuff<base::get_data()>();
else
do_stuff(base::get_data());
;
struct foo
static constexpr std::size_t get_data () return 1u; ;
struct bar
static std::size_t get_data () return 2u; ;
int main ()
derived<foo>.execute(); // print "constexpr case (1)"
derived<bar>.execute(); // print "not constexpr case (2)"
Not exactly what you asked (I've developer a custom type trait specific for a get_value()
static method... maybe it's possible to generalize it but, at the moment, I don't know how) but I suppose you can use SFINAE and make something as follows
#include <iostream>
#include <type_traits>
template <typename T>
constexpr auto icee_helper (int)
-> decltype( std::integral_constant<decltype(T::get_data()), T::get_data()>,
std::true_type );
template <typename>
constexpr auto icee_helper (long)
-> std::false_type;
template <typename T>
using isConstExprEval = decltype(icee_helper<T>(0));
template <typename base>
struct derived
template <std::size_t I>
void do_stuff()
std::cout << "constexpr case (" << I << ')' << std::endl;
void do_stuff (std::size_t i)
std::cout << "not constexpr case (" << i << ')' << std::endl;
void execute ()
if constexpr ( isConstExprEval<base>::value )
do_stuff<base::get_data()>();
else
do_stuff(base::get_data());
;
struct foo
static constexpr std::size_t get_data () return 1u; ;
struct bar
static std::size_t get_data () return 2u; ;
int main ()
derived<foo>.execute(); // print "constexpr case (1)"
derived<bar>.execute(); // print "not constexpr case (2)"
edited Mar 21 at 21:07
answered Mar 21 at 21:01
max66max66
38.3k74473
38.3k74473
2
This is madness, this use of the comma operator, the long/int overload... Have an upvote. :/
– matovitch
Mar 21 at 21:13
@matovitch - never underestimate the power of the comma operator }:‑)
– max66
Mar 21 at 21:16
Will this work on platforms wheresizeof(long)
is equal tosizeof(int)
?
– Gregory Nisbet
Mar 22 at 3:35
2
@GregoryNisbet - Yes. Because, for the language so for the compiler, they remain different types.
– max66
Mar 22 at 8:08
add a comment |
2
This is madness, this use of the comma operator, the long/int overload... Have an upvote. :/
– matovitch
Mar 21 at 21:13
@matovitch - never underestimate the power of the comma operator }:‑)
– max66
Mar 21 at 21:16
Will this work on platforms wheresizeof(long)
is equal tosizeof(int)
?
– Gregory Nisbet
Mar 22 at 3:35
2
@GregoryNisbet - Yes. Because, for the language so for the compiler, they remain different types.
– max66
Mar 22 at 8:08
2
2
This is madness, this use of the comma operator, the long/int overload... Have an upvote. :/
– matovitch
Mar 21 at 21:13
This is madness, this use of the comma operator, the long/int overload... Have an upvote. :/
– matovitch
Mar 21 at 21:13
@matovitch - never underestimate the power of the comma operator }:‑)
– max66
Mar 21 at 21:16
@matovitch - never underestimate the power of the comma operator }:‑)
– max66
Mar 21 at 21:16
Will this work on platforms where
sizeof(long)
is equal to sizeof(int)
?– Gregory Nisbet
Mar 22 at 3:35
Will this work on platforms where
sizeof(long)
is equal to sizeof(int)
?– Gregory Nisbet
Mar 22 at 3:35
2
2
@GregoryNisbet - Yes. Because, for the language so for the compiler, they remain different types.
– max66
Mar 22 at 8:08
@GregoryNisbet - Yes. Because, for the language so for the compiler, they remain different types.
– max66
Mar 22 at 8:08
add a comment |
template<auto> struct require_constant;
template<class T>
concept has_constexpr_data = requires typename require_constant<T::get_data()>; ;
This is basically what's used by std::ranges::split_view
.
add a comment |
template<auto> struct require_constant;
template<class T>
concept has_constexpr_data = requires typename require_constant<T::get_data()>; ;
This is basically what's used by std::ranges::split_view
.
add a comment |
template<auto> struct require_constant;
template<class T>
concept has_constexpr_data = requires typename require_constant<T::get_data()>; ;
This is basically what's used by std::ranges::split_view
.
template<auto> struct require_constant;
template<class T>
concept has_constexpr_data = requires typename require_constant<T::get_data()>; ;
This is basically what's used by std::ranges::split_view
.
answered Mar 21 at 21:48
cpplearnercpplearner
5,52722342
5,52722342
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55288555%2fc-check-if-statement-can-be-evaluated-constexpr%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Hmm, the body of a
if constexpr
will only be evaluated if the expression in theif constexpr
is true at compile time. Is that what you are looking for?– Jesper Juhl
Mar 21 at 20:13
1
But what if the test in the if constexpr([test]) is not evaluatable at compile time?
– Aart Stuurman
Mar 21 at 20:17
6
Maybe you can do something with
std::is_constant_evaluated
?– 0x5453
Mar 21 at 20:18
en.cppreference.com/w/cpp/language/if
– Jesper Juhl
Mar 21 at 20:18
2
@AartStuurman: What is
do_stuff
that it can run at compile time or runtime, but itself should not beconstexpr
? Wouldn't it make more sense to just make it aconstexpr
function, and pass it the value ofget_data
as a parameter?– Nicol Bolas
Mar 21 at 20:38