125 questions
0
votes
1
answer
101
views
Macro-free, MSVC-compatible std::format with check for extra argument
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 ...
10
votes
1
answer
244
views
Cannot use std::not_fn with immediate functions
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, ...
-6
votes
1
answer
93
views
Can an executable call a static library's non-inline consteval function when using LTO?
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)...
2
votes
1
answer
173
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
2
answers
184
views
Forward string literal to consteval function
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 ...
5
votes
2
answers
138
views
What effect does consteval have on linkage in C++?
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):
...
6
votes
2
answers
132
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(...
4
votes
1
answer
206
views
using throw in a constexpr or a consteval function in order to generate compile-time error
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 ...
0
votes
1
answer
128
views
Need help to understand how std::format can check its format string at compile-time
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 ...
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 ...
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;...
13
votes
0
answers
224
views
Cannot call constexpr template member function in consteval constructor
// gcc: ok
// clang: compile error
struct arg
{
consteval arg(const int i) // consteval
: i_(i)
{
chk<int>(i); // error!!
}
template <typename T>
...
4
votes
0
answers
117
views
Can I make a consteval function throw a deprecation warning for given arguments?
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 = ...
1
vote
1
answer
155
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 ...
3
votes
1
answer
182
views
How can a sprintf-like function in C++20/23 verify that the number of format specifiers matches the number of provided arguments at compile time?
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 ...
0
votes
0
answers
18
views
Printing std::source_location attributes in __cyg_profile_func_enter causes segfault?
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 <...
0
votes
0
answers
45
views
Can I wrap static_assert in different function? [duplicate]
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);
}...
0
votes
1
answer
139
views
Compile Time unique hash for variables
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(...)...
0
votes
0
answers
53
views
Using constexpr std::vector returned from consteval function in another consteval function [duplicate]
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 ...
10
votes
0
answers
313
views
Is this strange behavior from Clang a compiler bug?
This code
#include <memory>
consteval auto func1()
{
return std::make_unique<int>();
}
template <typename T>
consteval auto func2()
{
[](auto) -> int {
func1();
...
2
votes
1
answer
75
views
(Reverse) forwarding consteval-ness of constructor with perfect forwarding
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 ...
6
votes
1
answer
237
views
Usage of std::is_constant_evaluated() since C++23
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::...
0
votes
0
answers
154
views
Consteval error: taking address of an immediate function
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.
...
1
vote
0
answers
84
views
Pass const expression to consteval function through non-consteval functions
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' ...
1
vote
1
answer
147
views
this declaration has no storage class or type specifier - compiler not detecting consteval
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>
...
2
votes
1
answer
165
views
consteval member function allowed?
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() {
...
2
votes
1
answer
747
views
Calling a consteval function within if consteval causes error in non-constexpr context
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::...
0
votes
1
answer
191
views
C++23 constexpr size limitations? Iterating through 0x4000 = ok, to 0x5000 = not a constant expression
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] ...
1
vote
0
answers
68
views
Returning an array from a consteval function where size of an array is a function argument [duplicate]
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 ...
9
votes
0
answers
251
views
How can I get the compiler to warn me about extremely long symbol names?
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 ...
0
votes
0
answers
74
views
C++ Compile time check for existence of functions matching enum keys
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 ...
0
votes
0
answers
30
views
Compute std::vector in consteval context and copy values to std::array in C++20 or later [duplicate]
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 ...
2
votes
1
answer
213
views
Is the address of a consteval-constructed object a constant expression?
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 ...
0
votes
0
answers
96
views
C++: enforce a function argument is consteval
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......
2
votes
1
answer
260
views
GCC and Clang are behaving differently with respect to constant evaluation
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>
...
0
votes
0
answers
88
views
Manifestly constant-evaluated expressions: inconsistency between constexpr and consteval? [duplicate]
I'm comparing the behavior of constexpr and consteval:
#include <iostream>
#include <string_view>
[[nodiscard]] constexpr std::string_view DispCExpr(int) noexcept {
if (std::...
2
votes
0
answers
159
views
How different would be these two compile-time Factorial functions in C++20
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 ...
5
votes
1
answer
172
views
Why does a Template inside a consteval function accepts a function parameter as one of its template parameters?
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::...
5
votes
2
answers
962
views
Calculate 'const char *' string hash at compile-time
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 ...
3
votes
2
answers
202
views
Unable to update std::variant in consteval context
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 ...
0
votes
1
answer
135
views
Problem with consteval and multiplying magic numbers. c++
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;...
1
vote
2
answers
552
views
How can constexpr pointers exist and constevel function return a pointer at compile time?
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 ...
0
votes
1
answer
146
views
C++ function that returns a pointer to a member function
the following works as expected:
template<typename... Types>
auto countNumberOfTypes() { return sizeof...(Types); }
template<typename... Types>
consteval auto functionReturnsFunction() { ...
1
vote
1
answer
530
views
consteval: Call to consteval function 'encrypt_string' is not a constant expression, pointer to subobject of temporary is not a constant expression
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 ...
0
votes
0
answers
85
views
How do I issue a compiler error for invalid argument to a consteval function?
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>
...
2
votes
0
answers
61
views
Within a c++ consteval method, is it allowed to refer to the name of a static inline variable to enforce instantiation?
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
{
...
0
votes
1
answer
175
views
Is it possible to make a compile-time evaluated function return a different type than the runtime evaluation of the same function?
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 ...
2
votes
2
answers
1k
views
What are the advantages of using constexpr instead of consteval function?
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 (...
3
votes
2
answers
112
views
Is it possible to derive template parameters from a literal integer in a formula somehow?
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 ...
1
vote
2
answers
298
views
Is it possible to raise compile error when string is too long without macros in C++20 or lower?
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 ...