494 questions
5
votes
6
answers
233
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>...
1
vote
2
answers
170
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
564
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
174
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
216
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
275
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
109
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
131
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
191
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
449
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
77
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
175
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
140
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. ...
6
votes
6
answers
640
views
C++ constexpr operations on each element of a constexpr array
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
#...
1
vote
1
answer
114
views
Compile-time check for -fsingle-precision-constant
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 ...
3
votes
1
answer
307
views
gcc unexpected C static_assert error: expression in static assertion is not constant
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 <...
0
votes
0
answers
45
views
Trigger compile errors in constexpr functions [duplicate]
Considering codes below:
#include <cstddef>
#include <stdexcept>
template<size_t N>
class StaticBST {
private:
struct Node {
int value;
Node * left;
Node ...
0
votes
1
answer
166
views
static_assert vs C_ASSERT - which one to use? [closed]
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 ...
0
votes
1
answer
124
views
Is there a performance benefit to using c++'s std(::ranges)::uninitialized_... algorithms, and is it worth not having constexpr?
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 ...
3
votes
1
answer
268
views
Why does this static_assert on a pointer to an incomplete type in a templated function apparently work?
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 ...
1
vote
2
answers
156
views
Trigger (static_) assert when compiler is able to determine parameter constant at compile time
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)
{
...
2
votes
2
answers
640
views
Static assert in C function-like macro
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 - ...
1
vote
0
answers
270
views
How to do C++ Contracts such that compilers can use once support comes for this (such as `float f(X *x)[[expects:x]]` or `f(X *x) [[ensures: x->u]])`?
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")...
0
votes
4
answers
286
views
Can I detect (at compile time) whether I'm in an extern "C" {} block?
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 ...
0
votes
0
answers
59
views
Is it valid to `static_assert(false)` after handling all variant types in a visitor? [duplicate]
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> ...
1
vote
0
answers
70
views
How to debug compilation stage in C++ to check template specifications and type checkings [duplicate]
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>
#...
0
votes
1
answer
706
views
constexpr: Why my expression is not a constant expression?
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[...
0
votes
1
answer
88
views
in-expression compile-time check for constant expressions for older G++ [duplicate]
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 ...
0
votes
2
answers
134
views
Best practice implementation of functions-family in cpp
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 ...
-1
votes
1
answer
132
views
How to static_assert each data member of a C++ lambda?
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 ...
3
votes
2
answers
301
views
How to enforce the C++ named requirement "Container" [duplicate]
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:
...
1
vote
0
answers
90
views
How to use an array-literal with _Static_assert in C?
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 ...
0
votes
0
answers
129
views
static_assert usage: What am I missing here?
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 ...
0
votes
0
answers
75
views
Static assert fails in constexpr template [duplicate]
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&...
0
votes
2
answers
95
views
statically assert inheritance of base class template
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>
...
3
votes
2
answers
135
views
Why is static_assert breaking substitution?
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&...
1
vote
0
answers
71
views
static_assert presence of a member in an incomplete type
Given the following example:
#include <type_traits>
#include <vector>
#include <string>
template <typename T, typename = void>
struct has_name: std::false_type {};
template &...
0
votes
0
answers
278
views
Is there any reason to prefer asserting type traits over concepts?
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)
{
...
2
votes
3
answers
159
views
Is there something like templated static_asserts in C++?
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 ...
4
votes
1
answer
314
views
C: Assert that an expression is a string literal
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))
#...
0
votes
4
answers
318
views
Is there a way to detect padding bits in a bitfield?
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;...
4
votes
1
answer
1k
views
Check if a type is defined via static_assert?
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:
...
2
votes
1
answer
175
views
Determine if objects stored in an std::vector are trivially copyable
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::...
2
votes
2
answers
185
views
Verify an array of fixed-length character strings is sorted at compile time
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 ...
1
vote
3
answers
174
views
Can I fail compilation based on constexpr if?
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&...
1
vote
0
answers
73
views
Why does a nested class with an implicitly-defined default constructor not satisfy std::default_initializable? [duplicate]
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::...
3
votes
1
answer
2k
views
Is requires(std::derived_from<...>) equivalent to static_assert(std::is_base_of<...>)?
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 ...
1
vote
1
answer
730
views
GCC: _Static_assert "non-constant condition for static assertion"
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 ...
1
vote
1
answer
473
views
static_assert cause compilation errors while included in c file with linux GNU compilation
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 ...
1
vote
0
answers
128
views
Static assert failure while using boost::iostreams::gzip_decompressor()
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 ...