error::make_unique is not a member of ‘std’ - c++

I am trying to compile the following thread pool program posted on code review to test it.
https://codereview.stackexchange.com/questions/55100/platform-independant-thread-pool-v4
But I am getting the errors
threadpool.hpp: In member function ‘std::future<decltype (task((forward<Args>)(args)...))> threadpool::enqueue_task(Func&&, Args&& ...)’:
threadpool.hpp:94:28: error: ‘make_unique’ was not declared in this scope
auto package_ptr = make_unique<task_package_impl<R, decltype(bound_task)>> (std::move(bound_task), std::move(promise));
^
threadpool.hpp:94:81: error: expected primary-expression before ‘>’ token
auto package_ptr = make_unique<task_package_impl<R, decltype(bound_task)>>(std::move(bound_task), std::move(promise));
^
main.cpp: In function ‘int main()’:
main.cpp:9:17: error: ‘make_unique’ is not a member of ‘std’
auto ptr1 = std::make_unique<unsigned>();
^
main.cpp:9:34: error: expected primary-expression before ‘unsigned’
auto ptr1 = std::make_unique<unsigned>();
^
main.cpp:14:17: error: ‘make_unique’ is not a member of ‘std’
auto ptr2 = std::make_unique<unsigned>();
^
main.cpp:14:34: error: expected primary-expression before ‘unsigned’
auto ptr2 = std::make_unique<unsigned>();

make_unique is an upcoming C++14 feature and thus might not be available on your compiler, even if it is C++11 compliant.
You can however easily roll your own implementation:
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
(FYI, here is the final version of make_unique that was voted into C++14. This includes additional functions to cover arrays, but the general idea is still the same.)

If you have latest compiler, you can change the following in your build settings:
C++ Language Dialect C++14[-std=c++14]
This works for me.

1.gcc version >= 5
2.CXXFLAGS += -std=c++14
3. #include <memory>

This happens to me while working with XCode (I'm using the most current version of XCode in 2019...). I'm using, CMake for build integration. Using the following directive in CMakeLists.txt fixed it for me:
set(CMAKE_CXX_STANDARD 14).
Example:
cmake_minimum_required(VERSION 3.14.0)
set(CMAKE_CXX_STANDARD 14)
# Rest of your declarations...

In my case I was needed update the std=c++
I mean in my gradle file was this
android {
...
defaultConfig {
...
externalNativeBuild {
cmake {
cppFlags "-std=c++11", "-Wall"
arguments "-DANDROID_STL=c++_static",
"-DARCORE_LIBPATH=${arcore_libpath}/jni",
"-DARCORE_INCLUDE=${project.rootDir}/app/src/main/libs"
}
}
....
}
I changed this line
android {
...
defaultConfig {
...
externalNativeBuild {
cmake {
cppFlags "-std=c++17", "-Wall" <-- this number from 11 to 17 (or 14)
arguments "-DANDROID_STL=c++_static",
"-DARCORE_LIBPATH=${arcore_libpath}/jni",
"-DARCORE_INCLUDE=${project.rootDir}/app/src/main/libs"
}
}
....
}
That's it...

If you are stuck with c++11, you can get make_unique from abseil-cpp, an open source collection of C++ libraries drawn from Google’s internal codebase.

Related

asyncF.cpp:24:31: error: no member named 'async' in namespace 'std'; [duplicate]

I know that std::async is a C++11 thing but I am pretty sure that my compiler has C++11 support.
#include <future>
using namespace::std;
void functionToRun() {
// some code
}
int main() {
auto x = 2; // throws warning warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
std::future<void> metricLoggerFuture = std::async(std::launch::async, functionToRun); // throws error no member named 'async' in namespace 'std'
}
and if I use
std::future<void> metricLoggerFuture = async(std::launch::async, functionToRun); // error: use of undeclared identifier 'async'
Also g++ --version shows
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
I have gone through the following,
No member named 'size' in namespace 'std'
No Member named stoi in namespace std
Also,
OS: macOS Catalina
This code is part of a bigger project (wrapper over TigerVNC) so there is a makefile, but since auto complies without any issues I don't think C++11 is the issue and hence passing -std=c++11 in the CXXFLAGS and CPPFLAGS also doen't help.
Looks like you are not compiling with c++11, add -std=c++11 (or later) to your compiler command line or makefile.

Clang Apple C++11 correct setup for CMake

I am trying to configure CMake to compile for OS X Target.This is IMac with OS X Yosemite v 10.10.2
Clang version:
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.1.0
When I compile without putting any compiler flags I am getting one error for this chunk of code in one the sources:
static const char* LogLevelStr[] {
"TRACE " ,
"INFO " ,
"WARNING" ,
"ERROR " ,
"FATAL " ,
};
error: definition of variable with array type needs an
explicit size or an initializer
I am compiling this code on Windows and GCC and it is completely fine so I don't understand why Clang complains here.So I decided,maybe I have to set C++11 support flags because I use this standard in the code a lot.
Setting
set (CMAKE_CXX_STANDARD 11)
or
set(CMAKE_CXX_FLAGS " -std=c++11")
Adds even more weird errors like these:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:51:52:
error:
expected ';' at end of declaration list
_LIBCPP_INLINE_VISIBILITY operator bool() const _NOEXCEPT
^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:87:57:
error:
expected ';' at end of declaration swap(__bit_reference<_Cp> __x, __bit_reference<_Cp> __y) _NOEXCEPT
^ >/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolc>hain/usr/bin/../include/c++/v1/__bit_reference:87:58:
error:
C++ requires a type specifier for all declarations >swap(__bit_reference<_Cp> __x, __bit_reference<_Cp> __y) _NOEXCEPT
^ >/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__config:338:21:
note:
expanded from macro '_NOEXCEPT'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:89:10:
error:
expected '(' for function-style cast or type construction
bool __t = __x;
~~~~ ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:92:2:
error:
expected ';' after top level declarator } ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:1111:47:
error:
expected ';' at end of declaration list
_LIBCPP_INLINE_VISIBILITY __bit_iterator() _NOEXCEPT
The error block from above the compiler spits at the point it is trying to parse include
Now,I tried also to set:
set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++")
Same errors.
What do I miss here?
UPDATE:
I don't understand why some people marked this question for closing.Anyway,here is the problem in more details.I tried all those C++11 flags.I also added '=' to that static array.But most of the errors come after that.And it looks like root of those is .At the very first place where gets parsed it goes down into another class called __bit_reference and there at line 51 the compiler complains
on the following line
_LIBCPP_INLINE_VISIBILITY operator bool() const _NOEXCEPT
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__bit_reference:51:52:
Expected ';' at end of declaration list
Most of the other errors are also in some ways connected to stl containers.
So my question is still valid.How do I get my source code to compile with the latest Clang on OS X including C++11 support.I am trying to do that with Xcode and have the same issues.
Xcode compiler output(some of it):
CompileC
/Users/michaeliv/Library/Developer/Xcode/DerivedData/xxxxxTest-hdkkzwyyppywsjgmoyuphranqtok/Build/Intermediates/xxxxxxTest.build/Debug/xxxxxxTest.build/Objects-normal/x86_64/XXXMath.o
/Users/XXXXXXX/Documents/XXXXX/xxxxxx/src/XXXMath.cpp normal x86_64
c++ com.apple.compilers.llvm.clang.1_0.compiler
cd /Users/xxxxxx/Desktop/xxxxTest
export LANG=en_US.US-ASCII
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-x c++ -arch x86_64 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -std=gnu++11 -stdlib=libc++ ....
It's difficult to say for sure since you aren't posting a reproducible problem.
However, the code sample you posted contains a flaw:
static const char* LogLevelStr[] {
"TRACE " ,
"INFO " ,
"WARNING" ,
"ERROR " ,
"FATAL " ,
};
should be changed to
static const char* LogLevelStr[] = {
"TRACE " ,
"INFO " ,
"WARNING" ,
"ERROR " ,
"FATAL " ,
};
I'm not 100% on this as regards the C++11 standard, however, this is my understanding.
When you write static const char* foo[] = { "foo", "bar", "baz", }; this is aggregate initialization and not list initialization or any other kind. In your code sample, it looks like you are trying to initialize an array using C++11 list-initialization. However, to my knowledge this is not possible, and the "brace-or-equal" syntax described in chapter 8 of the standard does not apply.
This answer refers to "clause 8" to argue that list-initialization of C-style arrays is not permitted by the standard: Initializing a member array in constructor initializer
IMO that is the problem and if I were you I would use an = there, even if it would be allowed to omit it in C++11 or some future standard.

boost::multiprecision::float128 and C++11

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.

why doesn't clang++ compile the following code?

I have the following code:
#include <type_traits>
int main()
{
}
g++ file.cc -std=c++0x
works just fine.
However, I need to use clang++ for some reason.
When I try
clang++ file.cc -std=c++0x
I get a bunch of errors:
In file included from file.cc:1:
In file included from /usr/include/c++/4.4.4/type_traits:50:
/usr/include/c++/4.4.4/tr1_impl/type_traits:230:41: error: expected ')'
struct is_function<_Res(_ArgTypes......)>
^
/usr/include/c++/4.4.4/tr1_impl/type_traits:230:28: note: to match this '('
struct is_function<_Res(_ArgTypes......)>
^
/usr/include/c++/4.4.4/tr1_impl/type_traits:230:12: error: redefinition of 'is_function<type-parameter-0-0 (type-parameter-0-1, ...)>'
struct is_function<_Res(_ArgTypes......)>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.4.4/tr1_impl/type_traits:227:12: note: previous definition is here
struct is_function<_Res(_ArgTypes...)>
^
/usr/include/c++/4.4.4/tr1_impl/type_traits:233:29: error: type qualifier is not allowed on this function
struct is_function<_Res(_ArgTypes...) const>
....
clang++ --version gives:
clang version 2.8 (branches/release_28)
Target: x86_64-redhat-linux-gnu
Thread model: posix
Any ideas how to fix that? (-std=c++11 doesn't work, not recognized.)
Clang 2.8 does not support C++11 features, you need to upgrade your compiler to use them.

How to use const auto with STLport library

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).