2,653 questions
0
votes
1
answer
122
views
Is it well-formed to track POD class layouts at compile time?
I'm trying to generate compile-time info about my POD types. Getting the offset of each member has been challenging due to limitations like no pointer conversions in constexpr, but I think I've found ...
15
votes
1
answer
1k
views
Why can a lambda passed as an argument be used in a constant expression?
Why does this code compile?
template<typename Callable>
int foo(Callable callable)
{
static_assert(callable());
return 0;
}
static const auto r = foo([] { return true; });
Compiler ...
8
votes
2
answers
171
views
Calling new in a constexpr function
I am trying to write a factory function that creates a constexpr object that uses new. Is this even possible?
The following code does not compile:
aa.cpp: In function ‘int main()’: aa.cpp:23:39: in ...
3
votes
1
answer
129
views
Compilation fails when two static strings are present, but succeeds with just one
I was experimenting with constexpr std::string and I found that gcc and clang both compile this code (inspect it at compiler explorer here)
#include <string>
static constexpr std::string FIRST =...
2
votes
1
answer
170
views
Compile-time manipulation of std::source_location::current()
I want to write a function that can obtain caller's function name, or at least its length, at compile time. I think obtaining std::source_location::current() is close to what I want and currently I'm ...
2
votes
3
answers
230
views
C++: How to iterate over tuple in compile-time?
How to iterate over tuple in compile-time?
problem code:
#include <array>
#include <cstddef>
#include <tuple>
namespace {
class Solution {
public:
template <size_t ...
0
votes
1
answer
122
views
How to reconcile a constexpr function's argument being not constexpr?
A simple function like:
constexpr int f(int x) {
constexpr int y = x;
return y;
}
Seems to not be legal because x is not constexpr. This complaint is made even before I call the function, ...
1
vote
1
answer
158
views
Sharing constants between CPU and GPU in CUDA
I'd like to share some constants between CPU and GPU in order to allow for execution of the same code (wrapped in between) on either. That includes some compile-time parameters which are most ...
6
votes
2
answers
129
views
Constant expression error when consteval function called from another
I am trying to use the return value from a consteval function UniqueSize() inside another consteval function UniqueArra to declare an array, but this results in a compiler error saying that UniqueSize(...
2
votes
1
answer
114
views
Why won't MSVC allow me to index into this array in consteval function?
This compiles on Clang and GCC, but not MSVC:
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <array>
template <uint32_t N>
struct BitfieldBits
{
...
1
vote
1
answer
157
views
C++ builtin constexpr vs CUDA __constant__ for higher dimension array
I recently need to implement a Sobol sequence generator by CUDA.
Now to pre-store the Sobol table, I can either use a C++ native constexpr like below:
constexpr unsigned int sobol_table[NUM_DIMENSIONS]...
1
vote
1
answer
113
views
How to generalize concept with templated function?
Given something like the following:
struct numeric_limits_max_t {
template<class T>
constexpr operator T() const noexcept {
return std::numeric_limits<T>::max();
}
};
...
0
votes
1
answer
119
views
Is there any way to get `consteval`-like behavior in C++11?
I'm working on a project that must limit itself to the C++11 standard. There's one particular class I'm creating (some details below) whose most important method can, I think, often be evaluated at ...
4
votes
1
answer
152
views
C23 constexpr int cannot be defined using a constexpr float or double
The following code produces an error with gcc in the latest versions:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
constexpr double d = 2.2;
constexpr int i = d*10;
...
8
votes
2
answers
1k
views
Compiler is out of heap when compiling recursive consteval function
I'm writing a compile-time parser PoC (compiling a literal string to std::tuple) based on C++20 (or 23 if necessary). Currently gcc, clang, and MSVC all crash for this code, and MSVC gives useful ...
0
votes
0
answers
88
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 -...
6
votes
1
answer
202
views
Why is a recursive constexpr function so much faster with reference parameters than with values?
I have the following constexpr Fibonacci function:
constexpr auto fibo(unsigned int number) -> unsigned long long {
if (number < 2) return number;
return fibo(number - 1) + fibo(number - ...
0
votes
0
answers
54
views
constexpr function containing constexpr variable [duplicate]
If i have a constexpr function with a constexpr local variable its value must be known at compile time
constexpr int addSquare(int x) {
constexpr int multiplier = 2;
return x * x + multiplier;...
5
votes
1
answer
233
views
Can initialization of static constant be skipped by 'case' label?
I have a constant declared in one case of my switch statement:
void foo( int& v ) {
switch( v ) {
case 0:
static constexpr int c{ 0 };
break;
case 1:
v = c;
...
4
votes
2
answers
207
views
Can I detect an integer type without listing all of them as template specializations?
I am playing around with implementing C++ std library traits. I could implement is_integral like this:
template <typename T>
is_integral
{
static constexpr bool value = false;
}
template <&...
2
votes
1
answer
90
views
Should this example for non structural constexpr C++ argument compile?
I have the following code from Ben Deane talk.
#define CX_VALUE(...) [] { \
struct { \
constexpr auto operator()() const noexcept { return __VA_ARGS__; } \
using cx_value_tag = ...
3
votes
2
answers
125
views
Compile time check if string contains quotes
I want to assert that an object JsonKey if constructed with a string that is known at compile time doesn't contain a quote. Here's what I have:
LiveDemo
#include<iostream>
template <size_t N&...
2
votes
1
answer
359
views
static const vs contexpr in c23: what's the point?
c23 added the constexpr keyword to the standard, which seems to me to be quite puzzling.
I understand that the appeal would be that constexpr objects evaluate to constant expressions, whereas static ...
1
vote
1
answer
153
views
Mutable static variables in consteval contxt
I'm trying to implement a consteval lambda to compute the nth Fibonacci number. I want to create a result cache in the lambda which I can use if the result is already cached. Here's what I have up ...
1
vote
0
answers
49
views
check a c string at compile time in c++
I want to validate a c string at compile time (using static assert). I have already a constexpr function that is able to do. (I use C++17)
template <std::size_t N>
constexpr bool validate(const ...