125 questions
0
votes
1
answer
98
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 ...
-5
votes
1
answer
92
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
2
answers
171
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 ...
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(...
0
votes
1
answer
107
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
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;...
4
votes
0
answers
115
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 = ...
3
votes
1
answer
174
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
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
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 ...
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 ...
0
votes
0
answers
144
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
1
answer
146
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
726
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::...
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 ...
0
votes
0
answers
72
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 ...
2
votes
1
answer
210
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 ...
2
votes
1
answer
255
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>
...
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
2
answers
946
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 ...
0
votes
1
answer
126
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;...
0
votes
1
answer
142
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() { ...
0
votes
0
answers
79
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>
...
0
votes
1
answer
168
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 ...
3
votes
2
answers
109
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 ...