Questions tagged [static-assert]

An assertion statement that is verified at the compilation time. A feature of C++11/C++14, supported by gcc since 4.3

Filter by
Sorted by
Tagged with
0 votes
0 answers
50 views

in-expression compile-time check for constant expressions for older G++

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,150
0 votes
2 answers
106 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 ...
kaisong's user avatar
  • 149
-1 votes
1 answer
81 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 ...
Nathanael Weiss's user avatar
3 votes
2 answers
85 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: ...
TheMemeMachine's user avatar
1 vote
0 answers
65 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 ...
Abdulmalek Almkainzi's user avatar
0 votes
0 answers
71 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 ...
Vinod's user avatar
  • 1,041
0 votes
0 answers
60 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&...
user2683038's user avatar
0 votes
2 answers
63 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> ...
user avatar
3 votes
2 answers
98 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&...
Paulo's user avatar
  • 33
0 votes
0 answers
49 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 &...
Mircode's user avatar
  • 476
0 votes
0 answers
95 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) { ...
Joseph Garvin's user avatar
2 votes
3 answers
142 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 ...
Benjamin Bihler's user avatar
4 votes
1 answer
163 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)) #...
Kevin Meier's user avatar
  • 2,363
0 votes
4 answers
140 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;...
Caglayan DOKME's user avatar
3 votes
1 answer
284 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: ...
Tyler's user avatar
  • 313
2 votes
1 answer
97 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::...
user3749105's user avatar
2 votes
2 answers
108 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 ...
Nate Roiger's user avatar
0 votes
0 answers
43 views

Tensor Flow compilation in Visual Studio c++

I downloaded Tensor Flow by anaconda, set PATHs in the Visual studio C++ progect and trying to compile the "Hellow word" example from https://github.com/ksachdeva/tensorflow-cc-examples/blob/...
Zakharia Frenkel's user avatar
1 vote
3 answers
95 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&...
Chameleon's user avatar
  • 1,830
1 vote
0 answers
47 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::...
MarkB's user avatar
  • 710
2 votes
1 answer
293 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 ...
Casey's user avatar
  • 10.4k
1 vote
1 answer
166 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 ...
Torsten Robitzki's user avatar
1 vote
1 answer
163 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 ...
Tal V.'s user avatar
  • 31
1 vote
0 answers
73 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 ...
Hayk's user avatar
  • 27
0 votes
1 answer
173 views

Testing if an input iterator can write

I made this input iterator template <typename T> struct input_iter: base_it<T> { private: typename base_it<T>::pointer _ptr; public: ...
Alex Vergara's user avatar
  • 1,843
1 vote
1 answer
117 views

static_assert on an ill-formed-no-diagnostic-required expression [duplicate]

Is static_assert required to fail when processing an ill-formed-no-diagnostic-required (IFNDR) expression, or can the compiler ignore it and pass successfully the static_assert, as no diagnostic is ...
Amir Kirsh's user avatar
  • 12.9k
0 votes
0 answers
88 views

How to instantiatiate a std::basic_string_view<T> with custom class T, I got is_trivial_v<_CharT> assert error

I would like to construct a std::basic_string_view<T>, but the T is the customized class. Here is the test code: #include <string_view> #include <vector> struct Token { Token(); ...
ollydbg23's user avatar
  • 1,128
0 votes
0 answers
23 views

Domain name is undefined for assets duing build in angular SSR

While running serverless offline for my angular v14 application the assets domain for logo and styles are taking as undifined Images are not loading base href =
Mani kandan's user avatar
0 votes
0 answers
63 views

How can I make GCC Assert a Compile Time Error when an array of constant structs has a single uninitialized element?

I want to generate asserts at compile time, checking whether each element within a struct is initialized or not. I was wondering how I could generate a for loop of such static assert statements at ...
Timbino's user avatar
0 votes
1 answer
71 views

Maven Selenium Project - 'Cannot resolve symbol 'AssertionsForClassTypes' in IntelliJ

I am getting the following 2 errors in my Maven project:- 'Cannot resolve symbol 'AssertionsForClassTypes' 'Cannot resolve method 'assertThat' in 'SearchTest' In the pom.xml file, the following ...
JeannieW's user avatar
1 vote
1 answer
148 views

How to use static_assert inside of a constexpr

I have a function that can be called on an enum, save for one value. I want to check statically that the function is not called with that value. I have the following (simplified) (c++11) code: enum ...
Typhaon's user avatar
  • 878
1 vote
1 answer
166 views

How can I ensure at compile time that my serialization functions create buffers with the expected size?

Currently we use packed structs with bitfields to create our datas struct Data1 { uint8_t type; uint8_t value; bool aBool:1; uint8_t threeBits:3; uint8_t spare:4; } __attribute__((...
Bertrand Thelen's user avatar
0 votes
1 answer
79 views

How can I use static_assert to call a function that receives an array as pointer and length?

I have a function that receives an array, and I want to test it using static_assert(): // This is the function I want to test: constexpr static int find_minimum(const int arr[], size_t size); // the ...
DuduArbel's user avatar
  • 1,150
0 votes
5 answers
310 views

C++ run function at compilation time only [duplicate]

constexpr might run functions at compilation time. Is there a way to force it to compilation time only? Sample code: constexpr int BUILD(int i) { static_assert(0 == i); i++; return i; } ...
Ronen333's user avatar
-3 votes
2 answers
478 views

How can I print the type of an object as part of a static_assert statement?

I have written some type conversion operators which only make sense in the context of a subset of types. An example is below explicit virtual operator DataId<float>() const { static_assert(...
FreelanceConsultant's user avatar
3 votes
1 answer
129 views

Check if base class has specific constructor

Let's say we a base class with a specific constructor and a pure virtual member function. Is there a way to check if the base class has this constructor? struct Base { explicit Base(int); ...
yannick818's user avatar
0 votes
1 answer
149 views

Is it possible to make a compile-time (macros) branching based on assert condition? [duplicate]

For example, I want something similar in meaning to this: //Somethere in Windows header struct COUPLE { WORD part0; WORD part1; } //In my code #if sizeof(COUPLE) == sizeof(INT) #define ...
Alex A.'s user avatar
  • 422
0 votes
0 answers
86 views

How to assert execution of a function / How to assert memory does get uninitialised?

As we all know, it is good practice to assume the dumbest possible user. I am writing a library that does allocate memory in its use. I would like to make it impossible that this memory gets leaked, I ...
ZwergofPhoenix's user avatar
0 votes
2 answers
233 views

How to static_assert if a constexpr number is in a constexpr array at compile time?

I want to check if a constexpr number is in a constexpr array at compile time. If not, stop compiling. My ideographic example: constexpr void PinValid(uint8_t Pin) { constexpr uint8_t ValidPins[] =...
埃博拉酱's user avatar
0 votes
3 answers
279 views

Throw compiler error if const value is wrong (static assert)

I made a class for an arduino program. Inside the class I would like to toss a compiler error if a wrong pin number is passed as an argument. class AnalogOutput : public AnalogBlock { public: ...
bask185's user avatar
  • 387
2 votes
0 answers
51 views

Given `false_v<T> = false`, `static_assert(false_v<T>)` compiles inside `if constexpr (false)`. Is this legal? [duplicate]

Consider this code that static asserts false inside an if constexpr (false) statement. #include <iostream> template<typename T> struct False { static constexpr bool value = false; }; ...
jcarpenter2's user avatar
  • 5,333
0 votes
1 answer
139 views

Is it OK to use lambda function parameter as a constant expression?

Why in this example the first call doesn't compile and the second one compiles? consteval auto foo(auto x) { static_assert(x); } int main(){ foo(42); // error: non-constant condition for ...
cppbest's user avatar
0 votes
0 answers
130 views

How to implement own assert function that uses static_assert if constexpr evaluated [duplicate]

How to implement an own assert function (or macro) that uses static_assert in the constexpr case. The following naive approach fails: void constexpr ownAssert( bool assumption ) { if ( assumption ) ...
hpc64's user avatar
  • 35
1 vote
2 answers
330 views

static_assert not working inside class template definition

I'm trying to define a static member variable outside the class definition. It works as intended. But the static_assert that I placed inside the class definition does not compile for some reason. Why? ...
digito_evo's user avatar
  • 3,276
1 vote
2 answers
817 views

Static file on static folder not cached

I'm using sveltekit and trying to load static assets like css/js on my localhost, but it didn't cached like disk cache or memory cache, how can I resolve this? it should be cached by default right? ......
Maulana iman's user avatar
0 votes
2 answers
114 views

How can A and not A be both true when using static_assert

A very confusing situation involving some constexpr and type traits led me to think the value of an expression is true, when in fact it was both true and false. https://godbolt.org/z/McYMvxasT #...
haelix's user avatar
  • 4,315
2 votes
1 answer
69 views

Static Assert unexpected behaviour

Here is the code which has unexpected behaviour: #include<iostream> using namespace std; namespace n1{ namespace n2{ enum comparator_t { LT = 0, /*!< less-than < */ ...
user19584800's user avatar
0 votes
0 answers
79 views

How to statically assert an object can't be moved

For an implementation of a container class that keeps raw pointers to objects (intended for computing and enumerating subsets of objects temporarily), I want to statically assert that the objects ...
Anders's user avatar
  • 223
-1 votes
1 answer
492 views

Is there a way to static_assert a variable reference given in a template parameter?

struct Config { int version = 1; }; template<Config& config /* , ... */> struct Peripheral { const Config config_ = config; static_assert(config_.version > 1, "Config ...
Ghali's user avatar
  • 3
4 votes
2 answers
210 views

Static assert that method cannot be called from constructor or destructor

Suppose I have the following classes: template <typename SELF> class Base { protected: template <int X> void foo(); }; class Child : public Base<Child> { public: Child() ...
Patrick Wright's user avatar

1
2 3 4 5
10