Skip to main content
Stack Overflow for Teams is now Stack Internal: See how we’re powering the human intelligence layer of enterprise AI. Read more >
Filter by
Sorted by
Tagged with
5 votes
6 answers
233 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
1 vote
2 answers
170 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.3k
11 votes
1 answer
564 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,607
0 votes
2 answers
174 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.5k
1 vote
1 answer
216 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.3k
4 votes
4 answers
275 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
109 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
131 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
191 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
  • 459
1 vote
1 answer
449 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
  • 137k
2 votes
0 answers
77 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
175 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
140 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
6 votes
6 answers
640 views

Would be possible to iterate over a constexpr C-array/std::array/vector and execute constexpr operations on each element and everything to be done at compile time? https://godbolt.org/z/5a4v3Eerh #...
Solo's user avatar
  • 439
1 vote
1 answer
114 views

I learned from the accepted answer to Make C floating point literals float (rather than double) and it's discussion, GCC provides the compiler flag -fsingle-precision-constant to force floating point ...
Wolf's user avatar
  • 10.3k
3 votes
1 answer
307 views

I have some C code which builds OK with gcc-11 and earlier but not gcc-12 and later. This illustrates the problem (this isn't the actual code I'm working on): #include <stdint.h> #include <...
gebjon's user avatar
  • 131
0 votes
0 answers
45 views

Considering codes below: #include <cstddef> #include <stdexcept> template<size_t N> class StaticBST { private: struct Node { int value; Node * left; Node ...
NYA's user avatar
  • 9
0 votes
1 answer
166 views

I have a general C++ / Windows question for coding standards regarding compile-time assertions. static_assert is C++11 and language/compiler supported; C_ASSERT is a define from winnt.h So, if I have ...
Dominik Weber's user avatar
0 votes
1 answer
124 views

I'm implementing a container type that owns some memory which I am creating using std::make_unique_for_overwrite(). Given that this function is specifically returning a std::unique_ptr to ...
SHIPWECK's user avatar
  • 109
3 votes
1 answer
268 views

In the process of replacing a static_assert in a templated function with a requires statement, we discovered that firstly, the function was occasionally being used on an incomplete type, and that ...
James Picone's user avatar
  • 1,619
1 vote
2 answers
156 views

Consider the following code: Live demo #include <iostream> #include <fcntl.h> #include <assert.h> struct MyFile { MyFile(const char* filename, int flags, mode_t mode) { ...
glades's user avatar
  • 5,346
2 votes
2 answers
640 views

How to implement (compile-time) static assertion in function-like macros ? There is good discussion, and lot of alternative for the case of injecting static assertion as "C" statement - ...
dash-o's user avatar
  • 14.6k
1 vote
0 answers
270 views

Must you use a macro to wrap this (plus hope whoever uses your code years later knows to pass -D USE_CONTRACTS,) such as /* Licenses: allows all uses ("Creative Commons"/"Apache 2")...
Swudu Susuwu's user avatar
0 votes
4 answers
286 views

Say I have a C header file a.h Any C++ header file, say a.hpp that #includes a.h, should do so in an extern "C" block. So I have some header files that are common between our C++ code and C ...
John Carter's user avatar
0 votes
0 answers
59 views

An example on cppreference.com includes the following: ... // the variant to visit using var_t = std::variant<int, long, double, std::string>; ... int main() { std::vector<var_t> ...
Ground's user avatar
  • 58
1 vote
0 answers
70 views

I am writing a file parser. To avoid multiple implementations of one function, I wrote the following codes. Here is a minimal example: https://cppinsights.io/s/d7b81581 #include <string> #...
LiuYuan's user avatar
  • 493
0 votes
1 answer
706 views

I want to define a buffer at compile-time and check its size with a static_assert: struct Buffer{ int* array; size_t size; }; template<typename T, int size> struct ConstArray{ T arr[...
Xemuth's user avatar
  • 369
0 votes
1 answer
88 views

I have the following macros for certain compile-time checks: /* in-expression compile-time check evaluating to 0 */ #ifdef __cplusplus template<bool> struct Chk_sa; template<> struct ...
mirabilos's user avatar
  • 5,419
0 votes
2 answers
134 views

Preliminary A functions-family is a countable list of functions y=f_i(x), for a counter i=0,...,n-1 for some integer n. Minimum example I have a derived struct in c++ that shall define such a ...
kaisong's user avatar
  • 181
-1 votes
1 answer
132 views

How can one call static_assert for each data member of any given C++ lambda? What I'm trying to do is roll my own memcpy-able std::function and have realized that any lambdas must have trivially ...
Nathanael Weiss's user avatar
3 votes
2 answers
301 views

I am trying to make a template container class and I want it to conform to the "Container" named requirement as best I can. I am looking at this cppreference link and at the bottom it says: ...
TheMemeMachine's user avatar
1 vote
0 answers
90 views

I want to use an array-literal of literals in a _Static_assert, but I get a compiler error saying this is not a constant expression _Static_assert((int[]){2, 1, 0}[2], "err"); Is there a ...
Abdulmalek Almkainzi's user avatar
0 votes
0 answers
129 views

I would like to reference two earlier posts: why-is-comparing-two-parameters-of-a-constexpr-function-not-a-constant-condition how-does-this-function-template-deduce-the-size-of-an-array In the first ...
Vinod's user avatar
  • 1,215
0 votes
0 answers
75 views

When I try to compile the following code using clang the static assertion fails: #include <type_traits> template<typename T> int TypeTest() { if constexpr (std::is_same_v<bool, T&...
user2683038's user avatar
0 votes
2 answers
95 views

How can I achieve in the below code that the compiler will return the intended compilation error message from the assertion in mixer (cf option B)? #include<iostream> #include<type_traits> ...
user avatar
3 votes
2 answers
135 views

Please, consider the following C++14 code: #include <type_traits> template<typename T> class Bar { static_assert(std::is_destructible<T>::value, "T must be destructible&...
Paulo's user avatar
  • 33
1 vote
0 answers
71 views

Given the following example: #include <type_traits> #include <vector> #include <string> template <typename T, typename = void> struct has_name: std::false_type {}; template &...
mqnc's user avatar
  • 766
0 votes
0 answers
278 views

As far as I can tell the code below asserts the same thing in two different ways: #include <type_traits> template<class T, class U> concept nothrow_assignable = requires(T a, U b) { ...
Joseph Garvin's user avatar
2 votes
3 answers
159 views

It is possible to check for the existence of class member functions. An implementation of the check could be taken from this answer: https://stackoverflow.com/a/257382/2492801. Now a static_assert can ...
Benjamin Bihler's user avatar
4 votes
1 answer
314 views

I would like a static assertion to be sure that a given expression is a string literal. I tried this: #define SAME_TYPES(x, y) __builtin_types_compatible_p(typeof(x), typeof(y)) #...
Kevin Meier's user avatar
  • 2,634
0 votes
4 answers
318 views

I extensively utilize bit-fields in my C++ embedded application, and I have encountered a problem. Below is an example showcasing my usage: struct { uint8_t /* Reserved */ : 3; uint8_t foo : 3;...
Caglayan Dokme's user avatar
4 votes
1 answer
1k views

I have a situation where I have an enum that defines a list of jobs. enum class job_t { a, b, c, }; Elsewhere I have classes that subclass an interface like class job_interface { public: ...
Tyler's user avatar
  • 358
2 votes
1 answer
175 views

This code doesn't compile, since the static assert fails. #include <vector> #include <type_traits> class A { }; int main() { std::vector<A> v; static_assert(std::...
user3749105's user avatar
2 votes
2 answers
185 views

When trying to verify an array of fixed-length character strings is sorted at compile time, there is odd behavior in using strncmp. If the validation function references the global array, all values ...
Nate Roiger's user avatar
1 vote
3 answers
174 views

Is there a workaround to do something like this? if constexpr (std::floating_point<T>) {} else if constexpr (std::integral<T>) {} ... else static_failure("Feature expansion needed&...
Chameleon's user avatar
  • 2,239
1 vote
0 answers
73 views

In the following code, only Bar3 fails. What is the reason behind this static assertion failure? #include <concepts> struct Bar1 { Bar1() = default; bool val = false; }; static_assert(std::...
MarkB's user avatar
  • 2,210
3 votes
1 answer
2k views

I'm attempting to learn requires expressions as a stepping stone towards getting comfortable with the concepts mechanic. The easiest way I could come up with was to ease into it by replacing all my ...
Casey's user avatar
  • 11k
1 vote
1 answer
730 views

I have larger C code base, where I want to integrate some C++ code. The C++ code needs some declarations from the C code base. Compiler is currently GCC 6.3.1, but we might be able to update the ...
Torsten Robitzki's user avatar
1 vote
1 answer
473 views

I have static assert that validates structures size on compilation time in order to avoid padding issues, and make sure all my structures are aligned to 4. Some of my code is cpp files and some is c ...
Tal V.'s user avatar
  • 31
1 vote
0 answers
128 views

I have a large compressed file that I have to read and periodically need to change the position in the file when reading, that's why I want to make it seekable. I have two versions to declare the ...
Hayk's user avatar
  • 69

1
2 3 4 5
10