consteval Questions
1
Solved
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::is...
0
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, th...
1
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>...
Thyratron asked 26/2 at 15:31
1
Solved
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:...
2
Solved
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 both...
1
Solved
Since C++20, std::vector can be used in constant expressions. And as far as I known, current C++ permits dynamically allocate memory under the condition that any such allocation is deallocated by t...
Dele asked 11/6, 2023 at 11:15
3
Currently you cannot use static_assert to verify parameters of a constexpr function, even if all calls to it are indeed constexpr.
That makes sense because the compiler still has to create a non-co...
Niccolo asked 26/7, 2019 at 20:45
2
Solved
If I'm not mistaken, the arguments for a user defined literal are always known at compile time. In C++20 you can force functions to execute at compile time using consteval so that throw generates a...
Downtrend asked 25/3, 2023 at 0:52
2
Solved
While (re)implementing a simple constexpr map, I wrote this
(godbolt):
template <class key_type, class value_type, int N>
class flat_map
{
private:
struct pair
{
key_type key;
value_type ...
Ciri asked 12/2, 2023 at 18:9
1
Solved
struct A {
consteval A() {};
};
constexpr bool g() {
auto a = new A;
delete a;
return true;
}
int main() {
static_assert(g());
}
https://godbolt.org/z/jsq35WxKs
GCC and MSVC reject the pro...
Polymeric asked 17/1, 2022 at 19:17
3
I have a lambda that ignores its int parameter and always returns a constant.
If I mark it consteval, compilation fails because.
The compiler complains about invoking the consteval lambda with a no...
Airless asked 9/11, 2022 at 12:55
1
Solved
Consider that minimized code snippet:
#include <vector>
class Bar
{
public:
constexpr Bar() {}
};
consteval Bar foo()
{
return Bar();
}
int main()
{
std::vector<Bar> bars{ foo(), ...
Tamas asked 12/9, 2022 at 10:46
2
In the following struct definition, the constructor A(int) delegates its work to immediate-function constructor A():
struct A {
int i = 0;
consteval A() = default;
A(int) : A() {}
};
Clang acc...
Latoria asked 12/1, 2022 at 7:36
1
Solved
In the next program struct B has immediate consteval default constructor, which does not initialize i field. Then this constructor is used to make a temporary and its i field remains untouched:
str...
Zane asked 11/2, 2022 at 18:58
2
Solved
Why can't we use compile-time 'variables' in consteval functions as template parameters?
I was testing this code (https://godbolt.org/z/fe6hhbeqW)...
// Returns the nth type in a parameter pack of types (ommited for clarity)
// template <std::size_t N, typename...Ts>
// nth_type{...
Dogtired asked 29/1, 2022 at 13:7
1
struct A {
~A() {}
consteval A() {}
consteval auto f() {}
};
int main() {
A{};
//A{}.f(); //1
}
https://godbolt.org/z/4KPY5P7o7
This program is accepted by ICC, GCC and Clang, but rejected ...
Maice asked 12/1, 2022 at 17:39
1
Solved
In the following code struct A has immediate function default constructor, and an object of the struct is created in the dynamic memory be means of new A{}:
struct A {
consteval A() {}
};
int ma...
Kraut asked 17/1, 2022 at 15:24
1
Solved
struct A {
int i;
consteval A() { i = 2; };
consteval void f() { i = 3; }
};
constexpr bool g() {
A a;
a.f();
return true;
}
int main() {
static_assert(g());
}
https://godbolt.org/z/hafc...
Sublime asked 12/1, 2022 at 11:51
1
Solved
The following program
template<class T>
consteval auto foo(const T&) {
return 0;
}
template<class T>
consteval auto bar(const T& t) {
auto n = foo(t);
return n;
}
int main(...
Siddra asked 30/11, 2021 at 6:56
1
Solved
I am using the neat fmt library, which in its version 8, does compile-time checking of its format string if the compiler supports the relevant features.
I would, at some point, like to write the fo...
1
In C++20 a new feature was added to get the source location information: https://en.cppreference.com/w/cpp/utility/source_location
Here is a slightly modified example from that page, where an addit...
Grearson asked 13/8, 2021 at 10:25
1
Solved
C++23 is going to introduce if consteval. Where is this going to be used and how does it differ from constexpr if?
3
Solved
I have the following function:
template <size_t TSize>
consteval size_t indexOf(SomeEnum someEnum,
const std::array<SomeEnum, TSize> &arr) {
for (size_t i = 0; i < TSize; ++i) ...
1
Solved
Consider the following code. I can compile it with GCC 10.2.0 and Clang 11.0.0 (as expected):
#include <iostream>
template<int>
struct T {
static constexpr auto fun() noexcept { retu...
1
I know the difference in requirements, I am mostly interested in what benefits from code quality it brings.
Few things I can think of:
reader can just read the function signature and know that fun...
1 Next >
© 2022 - 2024 — McMap. All rights reserved.