Missing include gives no compile error on RedHat 6 - c++

This snippet can not be compiled, since std::accumulate is found in the header numeric, which is not included.
#include <algorithm>
#include <vector>
int main () {
std::vector<int> vec{ 1, 2, 3, 4 };
return std::accumulate(vec.begin(), vec.end(),0);
}
The compiler explorer gives me the correct error message
<source>(6): error: namespace "std" has no member "accumulate"
return std::accumulate(vec.begin(), vec.end(),0);
I am using RedHat 6 and the intel compiler version 18.0.3. If I compile it with this setting, I get no error and the result is fine. No warning is shown, even if -Wall is used.
My question is, why don't I get an appropriate error message?

why don't I get an appropriate error message?
Because one of the standard library headers <algorithm> or <vector> that you use for compilation do include <numeric> themselves. This is a common portability issue; your code happens to compile with a particular standard library implementation, but fails to build with another one. Library implementations are free to include standard headers in standard headers. Maybe some functionality in your <algorithm> was implemented using any of the <numeric> algorithms, and there you are.
The compiler error you encounter is the reason tools like include-what-you-use exist. Using iwyu her would add #include <numeric> to your snippet. Note also that no warning flags will influence the result of the compilation. Either you get a hard compiler error or nothing.

Related

‘numeric_limits’ is not a member of ‘std’

I am trying to compile an application from source, FlyWithLua, which includes the sol2 library.
I am following the instructions but when I run cmake --build ./build I get the following error:
In file included from /home/jon/src/FlyWithLua/src/FloatingWindows
/FLWIntegration.cpp:10:
/home/jon/src/FlyWithLua/src/third_party/sol2/./upstream/sol.hpp: In lambda function:
/home/jon/src/FlyWithLua/src/third_party/sol2/./upstream/sol.hpp:7194:59:
error: ‘numeric_limits’ is not a member of ‘std’
7194 | std::size_t space = (std::numeric_limits<std::size_t>::max)();
There are several other errors on the same line after this, but I guess they might just go away if I can solve this one.
there are several similar issues with the solution to add the following includes to the .hpp file
#include <stdexcept>
#include <limits>
the sol.hpp file includes the following imports:
#include <stddef.h>
#include <limits.h>
https://sol2.readthedocs.io/en/latest/errors.html gives some hints about the why the compiler might not recognize these includes:
Compiler Errors / Warnings
A myriad of compiler errors can occur when something goes wrong. Here
is some basic advice about working with these types:
If there are a myriad of errors relating to std::index_sequence, type traits,
and other std:: members, it is likely you have not turned on your C++14 switch for
your compiler. Visual Studio 2015 turns these on by default, but g++ and clang++
do not have them as defaults and you should pass the flag --std=c++1y or
--std=c++14, or similar for your compiler.
the src/CMakeList.txt file has the following line:
set(CMAKE_CXX_STANDARD 17)
I'm only faintly familiar with C/C++ and this all seems very complicated to me, but I'm hoping that there might be an easily recognizable cause and solution to this to someone more skilled.
cat /etc/*-release gives
DISTRIB_RELEASE=21.10
DISTRIB_CODENAME=impish
DISTRIB_DESCRIPTION="Ubuntu 21.10"
$ g++ --version
g++ (Ubuntu 11.2.0-7ubuntu2) 11.2.0
/home/jon/src/FlyWithLua/src/third_party/sol2/./upstream/sol.hpp:7194:59:
error: ‘numeric_limits’ is not a member of ‘std’
7194 | std::size_t space = (std::numeric_limits<std::size_t>::max)();
This error message implies that src/third_party/sol2/./upstream/sol.hpp header uses std::numeric_limits, but also that std::numeric_limits hasn't been defined. The simplest explanation for that is that the header that defines std::numeric_limits hasn't been included. In such case, the solution is to include the header that defines std::numeric_limits.
the sol.hpp file includes the following imports:
#include <stddef.h>
#include <limits.h>
This confirms the problem. Neither of those headers define std::numeric_limits.
https://sol2.readthedocs.io/en/latest/errors.html gives some hints about the why the compiler might not recognize these includes:
Those hints may apply to some other cases, but not this one. std::numeric_limits has been part of the C++ standard since the beginning, so language version has no effect on its existence.
Conclusion: According to the quoted error message, sol.hpp uses std::numeric_limits which is defined in the header <limits>, but according to you, it doesn't include that header. If this is the case, then this is a bug in the sol.hpp file. Correct solution would be to fix the sol.hpp file by including <limits> in that file before using std::numeric_limits.

C++11 exit() and abs() without including <cstdlib>?

Please tell me why this code is compiled with C++11 standard:
#include <iostream>
using namespace std;
int main()
{
abs(-12);
exit(1);
return 0;
}
it is without error g++ -std=++11 main.cpp
this error g++ main.cpp
if you remove #include <iostream> then get the error on exit() and abs() when the program is compiled.
Tell me why this happens, if I don't include <cstdlib>? And how to do that would compile with -std=c++11 was a warning or error?
The answer is really simple: the code compiles because under C++11 your iostream lib includes other libraries that have exit() and abs() defined. You won't get a warning or error for that. Why would you? After all cascade includes are supported.
However you should not depend on that since that's not a part of the standard and may break under different compiler. But I guess that as long as you stick to one compiler and one -std flag you're fine. Also fixing this by adding some includes should not be a big deal as well.

I'm including <cmath> but can't compile std::abs(double)

After re-installing Rad-Studio XE2 I'm finding that some code that used to compile no longer works. For example I get a compiler error on the following:
#include <cmath>
void MyClass::Rotate(double RotAngle){
bool NotRotated = std::abs(RotAngle) < 1;
... do something
}
with the following error:
[BCC32 Error] XXX.cpp(38): E2015 Ambiguity between 'std::abs(int) at c:\program files (x86)\embarcadero\rad studio\9.0\include\windows\crtl\stdlib.h:142' and 'std::abs(__int64) at c:\program files (x86)\embarcadero\rad studio\9.0\include\windows\crtl\stdlib.h:538'
This code used to compile, and obviously should, so what am I missing? Rad_studio has had all updates applied..
In this case we actually can learn a lot from the standard.
C++98: In 26.5/Table 80 and 81 we learn that abs is in <cstdlib> and NOT <cmath>. But then in 26.5 we have the contradictory statement In addition to the double versions of the math functions in <cmath>, C++ adds float and long
double overloaded versions of these functions, with the same semantics. which then lists abs as having additional overloads in <cmath> where the preceeding table said it should not be.
This is actually fixed in C++11 where 26.8/Table 119 clearly shows abs as a member of <cmath> as well as in <cstdlib> (although the added overloads for floating point types still appear exclusive to <cmath>.
As for your problem there are two likely situations:
You were compiling as C++11 before and are no longer doing so.
You were compiling C++98/03 but your standard libraries were updated to a version where <cmath> no longer implicitly includes <cstdlib> and your compiler was based on the table requirements rather than the implicit requirements of 26.5.
Most likely including <cstdlib> would fix the problem as would compiling in C++11 mode.

How to Make Visual Studio C++ 2010 Compilation Behave Like gcc/g++? (or vice-versa)

Say you've got the following simple main.cpp file:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
const string FILENAME = "foo.txt";
ifstream somefile(FILENAME);
populations.close();
return 0;
}
This compiles fine via Visual Studio C++ 2010.
However, on a Linux-based system, if I execute make main and compile, we get an expected error since we didn't call c_str() on the string constant, like so:
ifstream somefile(FILENAME.c_str());
As is commonly known, and described in this SO thread.
How can I get VS to behave like gcc/g++ and raise a compilation error for the code above? Or, how can I get gcc/g++ to behave like VS and compile the above without error? (Is it a simple matter of upgrading my gnu compiler?)
(I don't believe disabling compiler extensions is a solution, as I've done this and it still compiles without error.)
Visual Studio behaves correctly in this case with respect to the C++11 standard (it works on g++ now, too). I'm not sure why would you want to do this, but you'll probably need to edit MSVC's headers (not advisable and rather drastic).
Strange thing is though, that they don't write it in their documentation. Can you check which constructor is actually being called?
It is available as part of the newer c++ standard.
To disable, add
#define _HAS_CPP0X 0
at the top before your includes.

C++ cwchar error

I am trying to compile the regex_search function on the vxWorks gcc platform. I was testing with an example to see if I can use it without any issues. The example file includes the following three headers.
#include <string>
#include <map>
#include <boost/regex.hpp>
The errors I get are as follows
include/c++/3.4.4/cwchar:73: error: `::mbstate_t' has not been declared
include/c++/3.4.4/cwchar:114: error: `::btowc' has not been declared
.........
.........(similar "not defined" errors)
c++/3.4.4/cwctype:20: error: `::wctrans_t' has not been declared
c++/3.4.4/cwctype:20: error: `::wctype_t' has not been declared
.........
.........(similar "not defined" errors)
I want to know what cwchar and cwtype headers do. When I open them and see, they contain lot of preprocessor #ifdefs which seems to be checking for compiler specific information.
My supervisor asks me to turn them off. How can I do it?
If your supervisor wants you to turn off the cw-support you can try to define BOOST_NO_CWCHAR and BOOST_NO_CWCTYPE in your project settings.
see http://www.boost.org/doc/libs/1_37_0/libs/config/doc/html/boost_config/boost_macro_reference.html