Why not always use assert? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
If static_assert is restricted to compile-time evaluations, why not always stick with assert if it can handle both compile-time and run-time evaluated expressions?

assert(...) is ALWAYS evaluated at runtime, of course you can call it with a compile-time evaluated expression, but you will only first see the assertion at runtime.
Sometimes you wanna make sure something only compiles when a certain expression is true, thats when you use
static_assert(expression) which gives a compiler error if not fulfilled.
This is in direct spirit with "fail as early as possible" (and probably hard too ;-)

Related

why are c++ functions designed to has exceptions by default? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
If a c++ function has no exception, I have to write noexcept explicitly; if a c++ has exceptions, I don't have to write anything. Why not just the opposite? If a c++ function has exceptions, I have to write except explicitly; if a c++ has no exception, I don't have to write anything.
Writing a C++ function that you are 100 % certain will not throw an exception is far from easy and requires a thorough dedicated attention.
I consider I am allowed to write this noexcept keyword as the reward.

static __forceinline or __forceinline static [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Good morning, this always made me wonder (even tho it doesn't really matter) but which one is the correct way? Or is there none and both ways are fine?
static __forceinline T some_func ( )
or
__forceinline static T some_func ( )
It is important to order modifiers alphabetically, except if you are writing code on the solstice or equinox (during which it angers the sun microsystems god).
Other than that, use whatever order you wish, so long as you are willing to live with the consequences.
Which are none.
As you said yourself, both are legal and their results are identical so it comes down on personal preference.
For what it's worth in most codebases I've worked compiler specific keywords were always used before any standard C++ keywords which makes your 2nd snippet more common.
However as always with code style, just pick one you like most and be consistent. Consistency is what's important.

How to handle multiple OR condition in IF [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
bool A=false;
bool B=true;
if(!A || !B)
{
.....
}
In this condition when A is true, it didn't checks the B but I want to check B instead of A .
In this condition I want to execute if any one is true(A or B), But If A is true I want to check B also. is there any other logic to resolve this apart from using different if conditions?
You may turn off short-circut evaluation in your compiler. If you work with your own types, you may overload || and &&. Overloaded logical operators are not short-circuted.
Both things are really bad. Programmers expect logical operators to behave in certain way and are very likely to get super confused with this unexpected behavior. You should stick to short-circuting.

Is it common practice to "abuse" loops as goto [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
If in code there are many if-clauses and execution of code is not sensible on a previous error, is it OK to use a one-cycles loop for this? Simply for being able to exit the block with a break?
Like this:
do {
//..code
if (error1) break;
//..code
if (errorN) break;
//do finally something when no errors before
} while (false);
Yes, this is idiomatic, even if, perhaps, it was not the intended use for a do while loop. The source code for the linux kernel exploits this.
There's nothing unclear about it: while(false) does exactly what is says on the tin.
Yes it's a common technique to avoid deep nesting, and actually preferable to goto;.
From point of readability its way better than goto statements. The scope and code flow of the loop is well defined, and you don't need to lookup the corresponding labels of the goto statements, which not necessarily appear below.

C++ take a user input as the number of dimensions an array has? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I can't seem to find anything regarding whether or not this is possible. Ideally I'd have someone input an integer like "4" and then it would make a 4-d array.
is this at all possible?
As explained in the Stack Overflow post, "Create an N dimensional array, where N is determined at runtime (C++)":
The dimensions of an array are part of the type, and the type must be known
at compile time.
Thus the programmer must specify the dimensions of the array before compile-time, not during run-time.
Type checking is typically one of the first operations a compiler does (it is specifically found in the semantic analysis portion of the compiler's control flow) to ensure the code received is free of simple programming errors (assignment/equivilance errors, etc).
Please let me know if you have any questions!