This question already has answers here:
Constexpr Math Functions
(3 answers)
Closed 2 years ago.
I am trying to do something like:
#include <cmath>
template <unsigned A, unsigned B>
struct fu {
constexpr unsigned long power_A_of_B = std::pow(A, B);
};
But the compiler gives error because pow() is not constexpr.
Why are math functions in header <cmath> not constexpr?
For example, log(), log2(), pow(), abs() and fmax() are not constexpr but others in <algorithm> are, such as max(), min() and `clamp()'.
Why are math functions in header not constexpr?
Reason is well hidden in documentation of std::pow:
std::pow, std::powf, std::powl - cppreference.com
Error handling
Errors are reported as specified in math_errhandling.
See documentation of math_errhandling.
Now how this macro could be handled in a constexpr?
So whole problem is maintaining compatibility with old specification.
The only possible solution is used alternative implementations. I've found something like this, didn't tested it, but looks promising. Found here.
Related
we have following code in legacy and use this place hundread of place. I am compiling code with c++11 and got following error. I can understand issue(saw couple of question on stackoverflow) as abs support int/long int in C++11.
Is there any way to avoid 100 place and replace abs with fabs. Can I update such a way if it can handle both version. any input.
call of overloaded ‘abs(double&)’ is ambiguous
double abs(double d)
{
return (d < 0 ? -d : d);
}
Why redefine what is already defined?
You can find the function that you want in the header <cmath>.
Further reading on c++ reference.
#include <cmath>
add this header file and try to use abs() built-in function and see that work or not.
This question already has an answer here:
Is it a conforming compiler extension to treat non-constexpr standard library functions as constexpr?
(1 answer)
Closed 4 years ago.
I have written a c++ program as blow:
#include <iostream>
int main()
{
constexpr double a = 4.0;
constexpr double b = sqrt(a);
std::cout << b << std::endl;
return 0;
}
When I tried to compile this code with visual studio 2017, I got an error that says a function call must have a constant value in a constant expression. The bad line is "constexpr double b = sqrt(a);".
But when I used g++ to compile the same code, no error was reported.
What's the reason of the error? What's the different between g++ and vc++?
sqrt isn't a constexpr function so can't be used in a constexpr expression. GCC seems to have a special built in version of sqrt which is constexpr. Clang doesn't allow this code either:
https://godbolt.org/z/SvFEAW
sqrt is required to be not a constant expression so constexpr double b = sqrt(a); is not supposed to work. Clang does not build this code as well. You also need to include <cmath> header in order to use this function.
include cmath library since you using a sqrt() function
http://www.cplusplus.com/reference/cmath/
This question already has answers here:
error: unknown type name ‘bool’
(4 answers)
Closed 6 years ago.
I want to understand this error: syntax error before 'bool', on the following code:
typedef struct hdate{
date_arc_u date;
unsigned short time;
bool test;
}PACKED_ST horodate_a
When I change bool to another type there is no error.
I already use bool in others parts of the code without error.
I don't understand this error here ....
It's probably because you are writing C code, and the bool type does not exist in C. Your file extension is probably .c, not .cpp, and your code definitely looks like it was written in C.
Probably your C compiler doesn´t know about bool type.
You can try:
1- Including this at first #include <stdbool.h>
2- Declaring at first typedef enum bool { false, true };
This question already has answers here:
Undef a typedef in C++?
(3 answers)
Closed 7 years ago.
To avoid confusion and possible errors, is it possible to hide or undefine a typedef.
I do a lot of c++ and java at the same time. In java, the boolean type is boolean, and in c++ it's bool. The problem is that somewhere in the windows c++ libraries, there is a : typedef unsigned char boolean; That means that, in my c++ code, I mistype the bool type as boolean, it will compile and it could cause unexpected error, because it's an unsigned char instead of a true bool.
So what can I do to hide or undefine the boolean typedef in c++?
Yes it's possible with a precompiler directive like the example below:
typedef int foo;
#define foo not_to_be_used
int main() {
foo a = 1; // error: unknown type name 'not_to_be_used'
}
Basically, this way you cancel the typedef. The code above will issue an error if foo is used below the pre-compiler definition.
Live Demo
This question already has answers here:
How to detect existence of a class using SFINAE?
(4 answers)
Closed 6 years ago.
I'd need a template which can be called like this:
int x = type_exists< std::vector<int> >::value;
This should set x to 1 if #include <vector> was present (either explicitly or transitively) earlier in the source, otherwise it should set x to 0.
Is it possible to do it in C++? I'm using GCC, so GCC extensions are also fine.
It's also OK to change the call syntax a bit.
It's not OK to run the C++ compiler twice: first just to figure out if we get a compile error.
This is not what you are looking for, but it's as close as you can get to a type_exists trait:
template<class T> struct Void { typedef void type; };
template<class T, class U = void>
struct type_exists { enum { value = 0 }; };
template<class T>
struct type_exists<T, typename Void<T>::type> { enum { value = 1 }; };
Apparently, it works:
static_assert(type_exists<int>::value, "int is not defined");
static_assert(type_exists<SomeNonexistingType>::value, "expected compile-time error");
This does exactly what it is supposed to do. Tested with GCC 5.4.0.
This is not possible, I'm afraid. If we were to use a non defined identifier we would get a compilation error, leading to this code:
int x = type_exists< std::vector<int> >::value;
not to even compile.
Also, the standard doesn't specify any preprocessor directive to be declared within the header file (which is implementation defined instead) for the standard library, therefore you won't be able to detect it even with preprocessor macros.