How to use const auto with STLport library - c++

I've tried to compile elementary example:
#include <vector>
int main ()
{
std::vector<int> testV;
for (const auto& test : testV)
{ }
return 0;
}
And I've received error:
test.cpp: In function 'int main()':
test.cpp:5:29: error: 'begin' was not declared in this scope
test.cpp:5:29: error: 'end' was not declared in this scope
test.cpp:5:29: error: unable to deduce 'const auto&' from '<expression error>'
Does STLport support const auto ?
EDIT: I'm using GCC 4.6
With 4.7 and more everything is ok.

gcc 4.6 came out in the spring of 2011, was not without bugs in most C++11 features. Moroever, around the same time the rules for ADL lookup in range-for were also modified (note that this was prior to the official ratification of the C++11 Standard in the summer of 2011). See this Q&A for more details. It's probably not worth debugging this and the recommended course of action is to upgrade to a recent version of gcc (4.7 or preferably 4.8).

Related

What is the correct way to call std::to_chars?

I am trying to call std::to_chars on a double. My code doesn't compile on any compiler I have (gcc 10.2, clang 10.0.1).
Minimally reproducible code (or on godbolt):
#include <charconv>
#include <string>
int main() {
std::string str;
str.resize(30);
auto[ptr, ec] = std::to_chars(str.data(), str.data() + str.size(), double(3.5));
}
I compiled with -std=c++17.
The error I'm getting is:
On gcc 10.2:
<source>: In function 'int main()':
<source>:7:83: error: call of overloaded 'to_chars(char*, char*, double)' is ambiguous
7 | auto[ptr, ec] = std::to_chars(str.data(), str.data() + str.size(), double(3.5));
| ^
In file included from <source>:1:
/.../gcc-10.2.0/include/c++/10.2.0/charconv:366:1: note: candidate: 'std::to_chars_result std::to_chars(char*, char*, char, int)'
...
and then it lists all candidates.
On clang 10.0.1, it pretty much just tells me:
<source>:7:21: error: no matching function for call to 'to_chars'
auto[ptr, ec] = std::to_chars(str.data(), str.data() + str.size(), double(3.5));
^~~~~~~~~~~~~
I tried all the overloads with double that are listed on cppreference's page on std::to_chars, they all give different errors, but in the end nothing works.
I do not understand why the overload resolution is not clear when it should be clear, or am I doing something fundamentally wrong here?
I want to add that MSVC does it fine without any errors.
Your use is perfectly fine, and it compiles correctly under MSVC 19. As described in the Microsoft Docs for the <charconv> functions, this requires at least C++17.
As you can see for yourself in The GNU C++ Library Manual, Chapter 1. Status, C++ 2017, the last update regarding P0067R5 (Elementary string conversions, revision 5) was on GCC 8.1, with the comment "only integral types supported".
Similarly, the libc++ C++17 Status, defines P0067R5 as "Partially done".
If you need more evidence that this is a problem with your C++ implementation, look at the include file for your GCC/Clang installation and you will see that it doesn't define the floating-point overloads (they aren't defined for me, with GCC 10.2.1).

clang++ error on <experimental/any>

I get an error when compile code containing <experimental/any>.
Code in main.cpp:
#include <experimental/any>
int main() { }
Compile this (clang version is 3.9):
clang++ main.cpp -o main -std=c++1z
Error after the compiling:
In file included from main.cpp:2:
/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/experimental/any:364:34: error:
no template named '__any_caster'; did you mean 'any_cast'?
return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
^
/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/experimental/any:361:30: note:
'any_cast' declared here
inline const _ValueType* any_cast(const any* __any) noexcept
^
/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/experimental/any:372:34: error:
no template named '__any_caster'; did you mean 'any_cast'?
return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
^
/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/experimental/any:369:24: note:
'any_cast' declared here
inline _ValueType* any_cast(any* __any) noexcept
^
2 errors generated.
As #chris mentioned in the comments:
You could try with libc++. Perhaps there's an incompatibility with Clang in libstdc++'s new header.
This turns out to be true. Clang 3.9 is still experimental, and so it uses experimental headers, including a experimental C++ standard library. By default, it is provided by GCC, and so an incompatibility occurs between the GCC implementation and the Clang implementation.

C++11 : error: ‘begin’ is not a member of ‘std’

I am trying to do the following operation:
source = new int[10];
dest = new int[10];
std::copy( std::begin(source), std::end(source), std::begin(dest));
However, the compiler reports the following error.
copy.cpp:5434:14: error: ‘begin’ is not a member of ‘std’
copy.cpp:5434:44: error: ‘end’ is not a member of ‘std’
copy.cpp:5434:72: error: ‘begin’ is not a member of ‘std’
I have included the required <iterator> header in the code. Can anybody help me on this?
Template functions std::begin() and std::end() are not implemented for pointers (pointers do not contain information about the number of elements they refer to) Instead them you should write
std::copy( source, source + 10, dest);
As for the error you should check whether you included header
#include <iterator>
Also maybe your compiler does not support the C++ 2011 Standard.
In addition to include <iterator>in C++11 enabled compiler. You should know begin/end are not useful for pointers, they're useful for arrays:
int source[10];
int dest[10];
std::copy(std::begin(source), std::end(source), std::begin(dest));
also have this problem when using g++ compiler this code in linux.
Using g++ compiler that contain C++ featuer should add C++11 flag
g++ -std=c++11 -o test test.cpp

error: expected initializer before ‘:’ token

I am trying to compile some C++ code (which can be compiled with Visual Studio 2012 on Windows) with g++-4.4.
I have this snippet of code,
const std::string cnw::restoreSession(const std::vector<string> &inNwsFile) {
for (std::string &nwFile : inNwsFile){
// some...
}
}
that I cannot compile because of this error:
CNWController.cpp:154: error: expected initializer before ‘:’ token
Can you give me some advise on how to solve this problem?
Your compiler is too old to support range-based for syntax. According to GNU it was first supported in GCC 4.6. GCC also requires you to explicitly request C++11 support, by giving the command-line option -std=c++11, or c++0x on compilers as old as yours.
If you can't upgrade, then you'll need the old-school equivalent:
for (auto it = inNwsFile.begin(); it != inNwsFile.end(); ++it) {
std::string const &nwFile = *it; // const needed because inNwsFile is const
//some...
}
I believe auto is available in GCC 4.4 (as long as you enable C++0x support), to save you writing std::vector<string>::const_iterator.
If you really do need a non-const reference to the vector's elements then, whichever style of loop you use, you'll need to remove the const from the function parameter.

Range For loop help: Expected initializer before ":" token

I am completely new to C++ and I am now following the C++ Primer book.
I wrote a small example about strings, here is the code:
#include <iostream>
#include <string>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main() {
string s("Hello World");
for (auto &c : s)
c = toupper(c);
cout << s << endl;
return 0;
}
I am on Linux with GCC version 4.4.6 and I tried to compile this code with:
g++ test_strings.c -std=c++0x
but got the following errors:
test_strings.c: In function 'int main()':
test_strings.c:14: error: expected initializer before ':' token
test_strings.c:19: error: expected primary-expression before 'return'
test_strings.c:19: error: expected ')' before 'return'
I copied the program from the textbook, so I though it was a misspelling but after a check and trying searching on the web and updating my gcc the error reminds. Help will be greatly appreciated, thanks in advance.
As per the C++0x/C++11 Support in GCC page, you need to be running gcc 4.6 to get the range-for feature.
The 4.6 changes page contains:
Improved experimental support for the upcoming C++0x ISO C++ standard, including support for constexpr (thanks to Gabriel Dos Reis and Jason Merrill), nullptr (thanks to Magnus Fromreide), noexcept, unrestricted unions, range-based for loops (thanks to Rodrigo Rivas Costa), opaque enum declarations (thanks also to Rodrigo), implicitly deleted functions and implicit move constructors.
Since you're running gcc 4.4.6, it's not available to you.