Skip to main content
Filter by
Sorted by
Tagged with
0 votes
1 answer
101 views

I use std::format in my logging, and it largely works great. However, std::format ignores extra arguments which I do not like. E.g., std::format("x = {}", x, y) compiles, so I want a ...
bers's user avatar
  • 6,309
10 votes
1 answer
244 views

I try to apply std::not_fn wrapper to an immediate consteval function. But even the simplest C++20 example #include <functional> consteval bool f() { return false; } // ok in GCC and Clang, ...
Fedor's user avatar
  • 24.7k
-6 votes
1 answer
93 views

Let's say I have the following function in a static library libutil (with the definition in one of the library's translation units, so it can't be inlined while compiling the caller's translation unit)...
Torph's user avatar
  • 36
2 votes
1 answer
173 views

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 ...
GKxx's user avatar
  • 638
2 votes
2 answers
184 views

My api would require calling runtime(constFoo(str)) to get the final result, I wanted to shorten this because the real function names are kind of verbose. To do this I need to somehow wrap the ...
 sentientbottleofwine's user avatar
5 votes
2 answers
138 views

I have been looking at how different specifiers affect linkage but need some help understanding how consteval affects linkage. The working draft of the standard says consteval implies inline (source): ...
Curtis sitruc's user avatar
6 votes
2 answers
132 views

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(...
aep's user avatar
  • 1,737
4 votes
1 answer
206 views

The problem Using static_assert to generate compile-time error is not always easy because it requires a constant expression as first argument. I found, on StackOverflow, several example where throw ...
Oersted's user avatar
  • 3,834
0 votes
1 answer
128 views

Out of curiosity, and with hope of learning useful compile-time techniques, I'm trying to understand how std::format performs compile-time checking of its arguments. I started from the implementation ...
Oersted's user avatar
  • 3,834
0 votes
1 answer
119 views

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 ...
Dimitrije Kostic's user avatar
0 votes
0 answers
54 views

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;...
Blair Davidson's user avatar
13 votes
0 answers
224 views

// gcc: ok // clang: compile error struct arg { consteval arg(const int i) // consteval : i_(i) { chk<int>(i); // error!! } template <typename T> ...
celeta's user avatar
  • 131
4 votes
0 answers
117 views

I have a function like: consteval int func(int n) { ... } I would like to throw a deprecation warning if passed certain argument values, e.g. if the argument is less than 5: constexpr int a = ...
Fuz's user avatar
  • 325
1 vote
1 answer
155 views

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 ...
Setu's user avatar
  • 1,086
3 votes
1 answer
182 views

I’m trying to learn C++23 and thought a good exercise would be writing a sprintf()-like function that —- provided the given format string is a string literal —- does a compile-time check to ensure the ...
Crutch's user avatar
  • 33
0 votes
0 answers
18 views

I'm playing around with -finstrument-functions, and I'm running into things I don't understand Let's take the following dead-simple program // main.cc #include <iostream> #include <...
bleh's user avatar
  • 1
0 votes
0 answers
45 views

I'm looking into C++20 features and I just stumbled upon idea of wrapping static_assert into differently named function e.g. constexpr void staticCheck(bool bCondition) { static_assert(bCondition); }...
ligazetom's user avatar
  • 153
0 votes
1 answer
139 views

is there any ways of making unique hashes for each variable in compile time. C++ allows block scopes which can result in same named variables inside one function so i cannot just do: #define MACRO(...)...
A A's user avatar
  • 41
0 votes
0 answers
53 views

I have the following code and it works, but it is clunky due to double call to get_vec5 that is required. namespace sr = std::ranges; namespace sv = std::views; template<int n> consteval auto ...
NoSenseEtAl's user avatar
  • 30.9k
10 votes
0 answers
313 views

This code #include <memory> consteval auto func1() { return std::make_unique<int>(); } template <typename T> consteval auto func2() { [](auto) -> int { func1(); ...
LHLaurini's user avatar
  • 2,810
2 votes
1 answer
75 views

Let's say I have a class with a consteval constructor that ensures a valid value at compile time. (There's also a std::expected-returning factory method that validates its argument at runtime, but ...
rkjnsn's user avatar
  • 985
6 votes
1 answer
237 views

I understand that std::is_constant_evaluated() was useful to determine compile time evaluation in C++20. But since C++23 we have if consteval. However there is no mention of deprecation for std::...
wohlstad's user avatar
  • 36k
0 votes
0 answers
154 views

I was solving https://projecteuler.net/problem=206, and wanted a compile time (consteval) solution. My idea was to check every number, until the answer is found. However, my code does not compile. ...
PlsHelp's user avatar
  • 337
1 vote
0 answers
84 views

in C++ is it possible to somehow pass a constant argument through multiple functions and still use consteval functions for them? The code below does not compile to FieldAsInt because the 'key' ...
Cus's user avatar
  • 121
1 vote
1 answer
147 views

I have a question about why I'm getting the two problems, this declaration has no storage class or type specifier, and expected a ';' ln 3 col 11, of the code: Main.cpp: #include <iostream> ...
SkullHeadI's user avatar
2 votes
1 answer
165 views

I have this c++20/23 code: #include <cstddef> template <size_t N> class Foo { public: consteval size_t size() noexcept { return N; } size_t real_size() { ...
HCSF's user avatar
  • 2,711
2 votes
1 answer
747 views

The following code does not compile with g++ 14.1 or clang++ 18.1: #include <type_traits> consteval int plusone(int n) { return n+1; } constexpr int maybeplusone(int n) { if (std::...
user3188445's user avatar
  • 4,990
0 votes
1 answer
191 views

The following code works and prints the intersection as expected. However, changing max_codepoint from 0x4000 to 0x5000 and it stops working with the error: ConsoleApplication1.cpp(149, 30): [C2131] ...
DubbleClick's user avatar
1 vote
0 answers
68 views

Why is it not possible to create an array in a consteval function (and return it) if array size is not a function template argument but just a function argument? The argument must be known at compile ...
Rudolf Lovrenčić's user avatar
9 votes
0 answers
251 views

The following code compiles fine and generates a compile-time array consisting of not too many elements, even though the original make_sequence function generated a large sequence. In this case, the ...
Weijun Zhou's user avatar
  • 5,569
0 votes
0 answers
74 views

In the project I'm working on we have a large number of classes with methods named to match enum keys. This provides a means to call functions that can be dynamically detected. General example of ...
CJBrew's user avatar
  • 2,797
0 votes
0 answers
30 views

I am trying to utilize std::vector, which has constexpr member functions since C++20, for compile-time calculations with a statically knowable but non-obvious number of values and then carry those ...
user2426460's user avatar
2 votes
1 answer
213 views

EDIT: this has been reported as a llvm bug here. The following code compiles on GCC and MSVC but is rejected by Clang: struct X { const void *p_; consteval X() : p_{this} { } }; static ...
Andrea Agostini's user avatar
0 votes
0 answers
96 views

I would like my API to enforce that certain function parameters are compile-time constants. I have made a template wrapper template<class T> class ConstEval { public: template <typename......
gimmeamilk's user avatar
  • 2,120
2 votes
1 answer
260 views

I'm observing an inconsistency between GCC and Clang with respect to what is a constant evaluated context. I played with different situations: #include <iostream> #include <type_traits> ...
Oersted's user avatar
  • 3,834
0 votes
0 answers
88 views

I'm comparing the behavior of constexpr and consteval: #include <iostream> #include <string_view> [[nodiscard]] constexpr std::string_view DispCExpr(int) noexcept { if (std::...
Oersted's user avatar
  • 3,834
2 votes
0 answers
159 views

Following This link, I came up to this question. I'm trying to understand how different would be the targeting the following two compile-time functions by a C++20 compiler. consteval int Factorial(int ...
Iman Abdollahzadeh's user avatar
5 votes
1 answer
172 views

I tried to extract a minimal working example from my codebase: #include <concepts> enum class unit_type { }; template <typename Candidate> concept unit_c = requires() { requires std::...
Martin Fehrs's user avatar
  • 1,175
5 votes
2 answers
962 views

I have a function that calculates the hash of a string literal: inline consteval uint64_t HashLiteral(const char* key) { // body not important here... return 0; } In another function I need ...
ScratchingTheSurface's user avatar
3 votes
2 answers
202 views

I was writing a compile-time parser but I am stuck on a problem which I don't know how to solve in C++. I am using Microsoft Visual Studio Community 2019, version 17.8.3 (latest). This is the code ...
Aakash Gupta's user avatar
0 votes
1 answer
135 views

Alright, so I'm trying to make a function that will hash a string. consteval int hash_string(const char* str) { constexpr int magic_number = 13371337; int num1 = 1337; int num2 = 7331;...
Bard's user avatar
  • 35
1 vote
2 answers
552 views

I was going through the topics of constexpr and consteval and found the below, We can have pointers that are of type CONSTEXPR A CONSTEVAL function can return a pointer of a CONSTEXPR variable and ...
Akshay J R's user avatar
0 votes
1 answer
146 views

the following works as expected: template<typename... Types> auto countNumberOfTypes() { return sizeof...(Types); } template<typename... Types> consteval auto functionReturnsFunction() { ...
XORer's user avatar
  • 73
1 vote
1 answer
530 views

I want to create a simple string encryption in C++ using the consteval keyword. Previously, I used constexpr, which worked, but some strings were encrypted at runtime. So, I decided to switch to ...
na29's user avatar
  • 77
0 votes
0 answers
85 views

I wanted to implement utility literals for evaluating roots of numbers. I implemented suffix literals that return root functions. Here's my code: #include <cmath> #include <stdexcept> ...
Samuel Okechukwu's user avatar
2 votes
0 answers
61 views

Is this valid c++ 20 or does the code rely on an undefined behavior? #include <iostream> template<int id> struct Registration { consteval auto operator() () const noexcept { ...
eel76's user avatar
  • 117
0 votes
1 answer
175 views

Is it possible, at compile time, to determine if a call to a function (a constexpr function mainly) is compile-time evaluated and than just make another version of that function (like what a template ...
user avatar
2 votes
2 answers
1k views

This might seem the same question as What are the advantages of using consteval instead of constexpr function? but it is actually exactly the opposite: Now that we have C++20 consteval, what are the (...
DanRechtsaf's user avatar
3 votes
2 answers
112 views

I have a class that wraps an integer into a range of values known only to the compiler (and the developer), the limits are unknown at runtime. The class implements operators such that the limits ...
wolfjazz's user avatar
  • 191
1 vote
2 answers
298 views

I am writing a simple fixed length string struct. In runtime, if you assign strings that are too long, I will just silently truncate them. This is for embedded and the string are to be displayed on a ...
Tomáš Zato's user avatar