181 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
5
votes
1
answer
213
views
Discarded branch of C++ constexpr if fails compilation because it calls non-matching function
I have a function that takes some input data that's an arbitrary range and a variant. If both the variant type and the input data are strings I want to call a function that process the string.
The ...
1
vote
1
answer
222
views
How to achieve conditional compile time for optional structure members without #if ... #else ... #endif
I am designing a new embedded c++23 library that will have a few hundreds of optional features.
the global idea is like this:
I would expect that if constexpr(...) would remove code when flag is false....
0
votes
0
answers
89
views
Can a constexpr if statement be used as an appropriate substitute for a debug macro?
Is a debug_log written entirely as a if constexpr an appropriate (recommended) substitute for a traditional macro implementation of a debug log?
Consider the following code compiled with -O3 and -...
1
vote
1
answer
455
views
How do I make a "static assertion" which is only checked on one branch of an if constexpr?
Consider the following code:
enum { a = 1, b = 2 - a};
void foo() {
if constexpr(a == 1) { }
else { static_assert(b != 1, "fail :-("); }
}
naively, one might expect the ...
3
votes
0
answers
63
views
constexpr if with function parameter error with clang, gcc is ok
Here is the test code:
#include <type_traits>
enum class ShaderType:unsigned{
Vertex = 0,
Fragment = 1,
};
template<ShaderType type> struct ShaderTypeHolder{
static constexpr ...
2
votes
0
answers
88
views
Why is a dependent expression not diagnosed as an error in a false if-constexpr block even if it is always a semantic error
There seems to be a lot of confusion out there about if constexpr and the difference between dependent- and non-dependent expressions, particularly in the context of static_assert. Before CWG2518, ...
1
vote
2
answers
142
views
if constexpr to initiallize a const reference
I have an expensive operation that I want to call conditionally depending on the template type.
The naive function looks basically like this.
template <typename T>
void FooNaive( const std::...
3
votes
1
answer
149
views
When does type traits instantiate templates
Why doesn't the static_assert inside Foo get triggered in the following code ?
Doesn't the type trait inside if constexpr need to instantiate Foo<float> & Foo<char> ?
https://godbolt....
0
votes
0
answers
49
views
constexpr using concepts, passes for incorrect type [duplicate]
I need a templated function which, at compile time, has some behavior inside of it specialized depending on the template arguments. I attempted to approach this using an if constexpr, checking for ...
2
votes
1
answer
100
views
Using if-constexpr and concepts to detect an instantiation of a specific enriched policy type
In the Modern C++ Design book by Andrei Alexandrescu, there is a section on enriched policies which shows a class that implements a policy can provide optional member functions for an enriched policy. ...
8
votes
1
answer
217
views
Weird behaviour with 'if constexpr' and templates in MSVC
The code below behaves incorrectly.
When f<0> or f<5> is called, it prints as if k = true, but the if statement behaves as if k = false. When f<1> is called, it does the opposite.
...
0
votes
0
answers
68
views
Customization point between libraries
I'm working on a bigger project which involves multiple libraries statically linked against each other.
E.g.: I have a Library A and Library B. Library B statically links against Lib A.
The problem I'...
3
votes
1
answer
150
views
How can `if constexpr` be made SFINAE-friendly?
I have a code using "classic" SFINAE.
template<class M> auto power(M const& elem) -> decltype(std::norm(elem)) { return std::norm(elem); }
template<class M, class = std::...
3
votes
1
answer
149
views
Dependent name error in discarded if-constexpr instantiation of function template or generic lambda
From my understanding, dependent name lookup doesn't take place until template instantiation, and template invocations in a discarded if-constexpr statement are not instantiated. As such, I would ...
0
votes
2
answers
137
views
Function overloading depending on compile-time object state
Consider having a simple class, that writes integers to cout depending on its state:
#include <iostream>
class NotAWriter{
public:
NotAWriter& operator<<(const int& arg) {
...