495 questions
21
votes
1
answer
1k
views
Heisenbug where static_assert is satisfied but triggers other static_assert
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 &...
1
vote
2
answers
201
views
Statically verify a undo function is called
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()
}
#[...
6
votes
6
answers
283
views
How to ensure order of template parameter pack with static_assert?
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>...
2
votes
2
answers
186
views
Why can't this friend class access the private move constructor?
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>);
...
11
votes
1
answer
567
views
Is this concept satisfaction a bug in GCC or did I invoke undefined behavior?
Here's the test code:
#include <concepts>
template<typename T>
concept Printable = requires(T obj) {
{ obj.print() } -> std::same_as<void>;
};
template<Printable T>
...
0
votes
2
answers
178
views
Cannot define an `std::array` with `std::string` argument in Visual C++
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 ...
1
vote
1
answer
282
views
Suspicious comparison of 'sizeof(expr)' to a constant
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 [[...
4
votes
4
answers
285
views
Check at compile time that a function call triggers a `static_assert(false)`
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 ...
2
votes
1
answer
115
views
Template to check overflow and static_assert
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 ...
5
votes
2
answers
144
views
c++ force static_assert failure (or similar) without type deduction
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. ...
5
votes
1
answer
238
views
How to check a string literal in static_assert?
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 ...
1
vote
1
answer
459
views
How do I make a "static assertion" which is only checked on one branch of an if constexpr?
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 ...
2
votes
0
answers
93
views
Why is a dependent expression not diagnosed as an error in a false if-constexpr block even if it is always a semantic error
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, ...
1
vote
2
answers
220
views
static_assert rejected in consteval constructor [duplicate]
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....
1
vote
1
answer
158
views
Compile time check that only one instance of a class is instantiated in the same scope
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. ...