Skip to main content
21 votes
1 answer
1k views

I have pinned a strange compilation error to this minimal code example: #include <iterator> #include <type_traits> struct A { class iterator { private: int i = 0; // compiles if &...
phinz's user avatar
  • 2,101
1 vote
2 answers
201 views

I want to statically verify that every time foo() is called, undo_foo() is also called. My idea was something like this: fn foo() -> Guard {} fn undo_foo(g: Guard) { g.verified_drop() } #[...
konradbu's user avatar
6 votes
6 answers
283 views

Let's say I have the following code: #include <array> #include <string> #include <variant> using namespace std::literals; using known_types_t = std::variant<int, double, char>...
Miguel Horta's user avatar
2 votes
2 answers
186 views

I get an error at the static assert: #include <type_traits> template <typename element_t> class MyVector { public: static_assert(std::is_move_constructible_v<element_t>); ...
Zebrafish's user avatar
  • 16.9k
11 votes
1 answer
567 views

Here's the test code: #include <concepts> template<typename T> concept Printable = requires(T obj) { { obj.print() } -> std::same_as<void>; }; template<Printable T> ...
Annyo's user avatar
  • 1,617
0 votes
2 answers
178 views

This code builds successfully with Clang 20.1.10 and with GCC 15.1, but not with Microsoft Visual C++ 2022 version 17.14.0: #include <array> #include <string> int main() { static ...
Pietro's user avatar
  • 13.6k
1 vote
1 answer
282 views

I did a static_assert to make sure that a valid type was used. I don't know if I just should have checked if the type is void, or whether any type can have size zero (I think it can with [[...
Zebrafish's user avatar
  • 16.9k
4 votes
4 answers
285 views

I have a template function with several type parameters and depending on those types, the function "calls" an static_assert(false). I want to know when my code fails to compile for specific ...
Lluís Alemany-Puig's user avatar
2 votes
1 answer
115 views

I made some templates which are supposed to (1) test if numbers can be added without overflow, (2) add numbers and fail in case of overflow. The (1) test_sum inside static_assert compiles, so I ...
herhor67's user avatar
  • 119
5 votes
2 answers
144 views

I'm writing some OS-specific logic and trying to use constexpr values instead of the usual #define macros. This becomes an if constexpr chain where the final else needs to force compilation failure. ...
ryan0270's user avatar
  • 1,277
5 votes
1 answer
238 views

I want to check, that a string literal doesn't have some specific value. #include <assert.h> static_assert("aaa"[0] != 'a'); However, this results in this error: error: expression in ...
Caulder's user avatar
  • 483
1 vote
1 answer
459 views

Consider the following code: enum { a = 1, b = 2 - a}; void foo() { if constexpr(a == 1) { } else { static_assert(b != 1, "fail :-("); } } naively, one might expect the ...
einpoklum's user avatar
  • 139k
2 votes
0 answers
93 views

There seems to be a lot of confusion out there about if constexpr and the difference between dependent- and non-dependent expressions, particularly in the context of static_assert. Before CWG2518, ...
leekillough's user avatar
  • 1,117
1 vote
2 answers
220 views

struct test { int var; consteval test(int i) : var{i} { static_assert(i == 3); } }; int main() { constexpr test t{3}; } This is rejected with: $ g++ c.cpp -std=c++20 c....
curiousguy12's user avatar
  • 1,817
1 vote
1 answer
158 views

I have a class from an external library. Only one instance may exist in the same scope. I can assert(instance_counter<=1) that no problem in a wrapper class. But I want to be sure at compile time. ...
Patrick Fromberg's user avatar

15 30 50 per page
1
2 3 4 5
33