#include <string>
#include <tr1/regex>
#include "TextProcessing.h"
const std::string URL_PATTERN("((http://)[-a-zA-Z0-9#:%_\\+.~#?&//=]+)");
const std::string REPLACEMENT("$&");
std::string textprocessing::processLinks(const std::string & text)
{
// essentially the same regex as in the previous example, but using a dynamic regex
std::tr1::regex url(URL_PATTERN);
// As in Perl, $& is a reference to the sub-string that matched the regex
return std::tr1::regex_replace(text, url, REPLACEMENT);
}
I'm using mingw gcc copiler 4.5.0 version.
I've got the follow error:
%DEV%> mingw32-make all
g++ -Wl,--enable-auto-import -Wall -Wextra -std=c++0x -pedantic -Werror -c -I./include TextProcessing.cpp
cc1plus.exe: warnings being treated as errors
c:\dev\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/tr1_impl/regex:2390:5: error: inline function '_Out_iter std::tr1::regex_replace(_Out_iter, _Bi_iter, _Bi_iter, const std::tr1::basic_regex<_Ch_type, _Rx_traits>&, const std::basic_string<_Ch_type>&, std::tr1::regex_constants::match_flag_type) [with _Out_iter = std::back_insert_iterator<std::basic_string<char> >, _Bi_iter = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, _Rx_traits = std::tr1::regex_traits<char>, _Ch_type = char, std::tr1::regex_constants::match_flag_type = std::bitset<11u>]' used but never defined
mingw32-make: *** [TextProcessing.o] Error 1
Process mingw32-make exited with code 2
Yup, in my g++ include files I also see that regex_replace is declared and not defined. For me, it's in the file /usr/include/c++/4.4.4/tr1_impl/regex . Just above the declaration is a Doxygen comment section which includes:
/** #todo Implement this function. */
Your standard C++ library does not have complete c++0x (now c++1x?) support. Try using Boost's code. See also this answer.
Related
I am able to compile the following a std=c++11 required code with g++ using the following command:
g++ test.cpp -std=c++11 -Wl,-rpath,/share/apps/gcc/6.3.0/lib64
the code:
#include <chrono>
#include <map>
#include <memory>
#include <thread>
#include <utility>
int main() {
typedef std::unique_ptr<int> intPointer;
intPointer p(new int(10));
std::map<int, std::unique_ptr<int>> m;
m.insert(std::make_pair(5, std::move(p)));
auto start = std::chrono::system_clock::now();
if (std::chrono::system_clock::now() - start < std::chrono::seconds(2))
{
std::thread t;
}
}
The very same command(probably I don't know the correct one) wont work for intel compiler:
icpc test.cpp -std=c++11 -Wl,-rpath,/share/apps/intel/2016.1.056/vtune_amplifier_xe_2016.1.1.434111/target/linux64/lib64
The error is:
In file included from /share/apps/gcc/6.3.0/include/c++/6.3.0/map(60),
from test.cpp(2):
/share/apps/gcc/6.3.0/include/c++/6.3.0/bits/stl_tree.h(1437):
error: identifier "_Compare" is undefined
&& is_nothrow_move_assignable<_Compare>::value)
^
In file included from /share/apps/gcc/6.3.0/include/c++/6.3.0/map(60),
from test.cpp(2):
/share/apps/gcc/6.3.0/include/c++/6.3.0/bits/stl_tree.h(1778):
error: identifier "_Compare" is undefined
_GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
^
What am I doing wrong, and how should I fix this.
From the comments above:
_Compare is one of the template parameters of the _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc> defined in that file, so surely it is defined.
A more likely reason is that the Intel compiler perhaps doesn't know about is_nothrow_move_assignable which is kind of recently added to the type_traits.
I had a user with a similar c++ difficulty using gcc-6.3.0 headers and the Intel 16.1.150 compiler. I opted for the compiler bug theory, tried with the Intel 17 compiler and things worked.
-doug
Well I checked with Intel agents and it turned out that Intel 16 do not support gcc6.3, the support comes in Intel 17.
I am porting some C++ code from Windows to Linux (and eventually OSX). Tons of C++ issues arise due to Windows non-compliance. I seem to have gotten past that, but am now facing a boost problem.
Basically, I want to chop up a string where the substrings of interest are separated by commas, and shove those into a string vector. This results in errors in g++, but it works fine compiling with Visual Studio
This program illustrates the issue exactly:
#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
int main (void) {
std::vector<std::string> str_vec;
std::string str_to_split = "this,is,the,string,to,split";
boost::algorithm::split(str_vec,
str_to_split.substr(1, str_to_split.size()-2),
boost::algorithm::is_any_of(","),
boost::algorithm::token_compress_on);
return 0;
}
To compile I do: >> g++ -o foo foo.cpp
This is my terminal output:
foo.cpp: In function 'int main()':
foo.cpp:11:54: error: invalid initialization of non-const reference type 'std::basic_string<char>&' from an rvalue of type'std::basic_string<char>'
boost::algorithm::split(str_vec,str_to_split.substr(1, str_to_split.size()-2),boost::algorithm::is_an
^
In file included from /usr/include/boost/algorithm/string.hpp:23:0,
from foo.cpp:1:
/usr/include/boost/algorithm/string/split.hpp:140:35: note: initializing argument 2 of 'equenceSequenceT& boost::algorithm::split(SequenceSequenceT&, RangeT&, PredicateT, boost::algorithm::token_compress_mode_type) [with SequenceSequenceT = std::vector<std::basic_string<char> >; RangeT = std::basic_string<char>; PredicateT = boost::algorithm::detail::is_any_ofF<char>]'
inline SequenceSequenceT& split(
^
This function takes std::string& rather than std::string or const std::string&. That means you'll have to store the result of .substr in an intermediate variable then pass the variable to boost::algorithm::split. It'll be clearer code anyway.
FWIW, I have no idea why the function is designed this way. Seems odd to me but there you go.
I'm trying to use boost::multiprecision::float128 (boost 1.55.0) under C++11 (gcc 4.8.1), but get the following compiler error:
/cm/shared/apps/boost/gcc/1.55.0/include/boost/multiprecision/float128.hpp: In static member function ‘static std::numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ET> >::number_type std::numeric_limits<boost::multiprecision::number<boost::multiprecision::backends::float128_backend, ET> >::min()’:
/cm/shared/apps/boost/gcc/1.55.0/include/boost/multiprecision/float128.hpp:533:55: error: unable to find numeric literal operator ‘operator"" Q’
static number_type (min)() BOOST_NOEXCEPT { return 3.36210314311209350626267781732175260e-4932Q; }
Can't I use boost::multiprecision::float128 in C++11? Or how else do I get it working?
edit
Just to clarify. This error is generated by
#include <boost/multiprecision/float128.hpp>
The compiler is not happy with the statement
return 3.36210314311209350626267781732175260e-4932Q;
in particular the Q is confusing it. I used the compiler flags -std=c++11 -fabi-version=0 -march=native -mfpmath=sse
It looks like a known issue. Try compiling with -fext-numeric-literals.
I want to split a wstring into a vector<wstring> using a single separator character.
This character is defined in a header file as a single char.
In order to keep the code clean and readable, I would really like to do this on a single line :)
I could not find a predicate to use, so I decided to use a C++11 lambda.
#include <boost/algorithm/string/split.hpp>
#include <vector>
#include <string>
constexpr char separator = '.'; // This is how it's declared in some header file
int main()
{
std::wstring text( L"This.is.a.test" );
std::vector<std::wstring> result;
// can't use is_any_of() unless i convert it to a wstring first.
boost::algorithm::split( result, text, [](wchar_t ch) -> bool { return ch == (wchar_t) separator; });
return 0;
}
Unfortunately, this results in a compilation error (clang 3.3):
clang++ -c -pipe -fPIC -g -std=c++11 -Wextra -Wall -fPIE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -I/usr/include -I/usr/lib64/qt5/mkspecs/linux-clang -I../splittest -I. -o debug/main.o ../splittest/main.cpp
In file included from ../splittest/main.cpp:1:
In file included from /usr/include/boost/algorithm/string/split.hpp:16:
/usr/include/boost/algorithm/string/iter_find.hpp:148:13: error: non-type template argument refers to function 'failed' that does not have linkage
BOOST_CONCEPT_ASSERT((
^~~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/concept/assert.hpp:44:5: note: expanded from macro 'BOOST_CONCEPT_ASSERT'
BOOST_CONCEPT_ASSERT_FN(void(*)ModelInParens)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/concept/detail/general.hpp:70:6: note: expanded from macro 'BOOST_CONCEPT_ASSERT_FN'
&::boost::concepts::requirement_<ModelFnPtr>::failed> \
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/algorithm/string/split.hpp:146:40: note: in instantiation of function template specialization 'boost::algorithm::iter_split<std::vector<std::basic_string<wchar_t>, std::allocator<std::basic_string<wchar_t> > >, std::basic_string<wchar_t>, boost::algorithm::detail::token_finderF<<lambda at ../splittest/main.cpp:13:44> > >' requested here
return ::boost::algorithm::iter_split(
^
../splittest/main.cpp:13:23: note: in instantiation of function template specialization 'boost::algorithm::split<std::vector<std::basic_string<wchar_t>, std::allocator<std::basic_string<wchar_t> > >, std::basic_string<wchar_t>, <lambda at ../splittest/main.cpp:13:44> >' requested here
boost::algorithm::split( result, text, [](wchar_t ch) -> bool { return ch == (wchar_t) separator; });
^
/usr/include/boost/concept/detail/general.hpp:46:17: note: non-type template argument refers to function here
static void failed() { ((Model*)0)->constraints(); }
^
1 error generated.
Am I doing something wrong or are C++11-lambdas not (completely?) supported in boost?
Is there another somehow readable single line solution?
I am currently using an own predicate is_char() which I defined in some base library, but I would rather get rid of it.
I know of boost lambdas (haven't used them, yet) - but should they really be used in C++11 code?
Thanks!
Out on a limb, try defining this before including the first boost header (beware of precompiled headers):
#define BOOST_RESULT_OF_USE_DECLTYPE
Or - conversely
#define BOOST_RESULT_OF_USE_TR1
I believe the defaults have changed. Recently. For specific compilers, so this could well explain it.
MinGw 4.7.0 Boost 1.47
code:
#include <iostream>
#include <boost/thread.hpp>
int main(int argc, char *argv[])
{
std::cout<<"In main"<<std::endl;
}
Compiler string:
g++.exe -Wall -fexceptions -g -IC:\soft\ides_comp\mingw\include\boost_1_47_0 -c D:\work\cpp_cb\mt1\main.cpp -o obj\Debug\main.o
It doesnt compile and i have same error whatever compiler options i tried.
Warning:
In file included from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/win32/thread_data.hpp:12:0,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/thread.hpp:15,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread.hpp:13,
from D:\work\cpp_cb\mt1\main.cpp:2:
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/win32/thread_heap_alloc.hpp:59:40: warning: inline function 'void* boost::detail::allocate_raw_heap_memory(unsigned int)' declared as dllimport: attribute ignored [-Wattributes]
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/win32/thread_heap_alloc.hpp:69:39: warning: inline function 'void boost::detail::free_raw_heap_memory(void*)' declared as dllimport: attribute ignored [-Wattributes]
Error:
In file included from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/smart_ptr/shared_ptr.hpp:30:0,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/shared_ptr.hpp:17,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/date_time/time_clock.hpp:17,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/thread_time.hpp:9,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/win32/thread_data.hpp:10,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/thread.hpp:15,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread.hpp:13,
from D:\work\cpp_cb\mt1\main.cpp:2:
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/checked_delete.hpp: In instantiation of 'void boost::checked_delete(T*) [with T = boost::error_info<boost::tag_original_exception_type, const std::type_info*>]':
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/smart_ptr/detail/shared_count.hpp:95:13: required from 'boost::detail::shared_count::shared_count(Y*) [with Y = boost::error_info<boost::tag_original_exception_type, const std::type_info*>]'
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/smart_ptr/shared_ptr.hpp:183:50: required from 'boost::shared_ptr<T>::shared_ptr(Y*) [with Y = boost::error_info<boost::tag_original_exception_type, const std::type_info*>; T = boost::error_info<boost::tag_original_exception_type, const std::type_info*>]'
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/exception/info.hpp:171:69: required from 'const E& boost::exception_detail::set_info(const E&, const boost::error_info<Tag, T>&) [with E = boost::unknown_exception; Tag = boost::tag_original_exception_type; T = const std::type_info*]'
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/exception/info.hpp:192:46: required from 'typename boost::enable_if<boost::exception_detail::derives_boost_exception<E>, const E&>::type boost::operator<<(const E&, const boost::error_info<Tag, T>&) [with E = boost::unknown_exception; Tag = boost::tag_original_exception_type; T = const std::type_info*; typename boost::enable_if<boost::exception_detail::derives_boost_exception<E>, const E&>::type = const boost::unknown_exception&]'
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/exception/detail/exception_ptr.hpp:182:13: required from 'void boost::unknown_exception::add_original_type(const E&) [with E = std::exception]'
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/exception/detail/exception_ptr.hpp:161:32: required from here
Warning 2:
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/checked_delete.hpp:34:5: warning: deleting object of polymorphic class type 'boost::error_info<boost::tag_original_exception_type, const std::type_info*>' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]
What can it be? Thanks
This has been fixed in later boost versions. Check here:
https://svn.boost.org/trac/boost/ticket/6165
Update boost/config/stdlib/libstdcpp3.hpp:
#if defined(_GLIBCXX_HAVE_GTHR_DEFAULT) \
|| defined(_GLIBCXX_HAS_GTHREADS) \
|| defined(_GLIBCXX__PTHREADS) \
|| defined(WIN32)
Added _GLIBCXX_HAS_GTHREADS and/or WIN32 (from the link above, see patch).
WIN32 seems a bit brute force, but doesn't work without (at least not for me. I am using an older boost version 1.42).
If you compiled the thread library for using Win32 threads, you might need
-DBOOST_USE_WINDOWS_H
in your compiler flags. While I don't think that's the issue (I get a different set of errors for not using it), you might give it a try.
This answer maybe not very straight, but:
I do use g++ 4.7.0 20111209 and it has support for threads.
#include <iostream>
#include <thread>
using namespace std;
void foo()
{
cout << "Hello from thread\n";
}
int main()
{
thread th1(foo);
th1.join();
return 0;
}
During compilation use -std=c++11 option to use the latest standard which supports threads
PS:
boost 1.48 compiled on my pc with mingw 4.7.0 gives an error (when trying to use boost::thread):
Threading support unavailable: it has been explicitly disabled with BOOST_DISABLE_THREADS
So I guess it was disabled by default with g++ 4.7.0 (with g++ 4.6.2 it was not).