c++-standard-library

Clang 3.1 + libc++ Compile Error

邮差的信 提交于 2019-12-22 09:39:59
问题 I've built and installed (under the prefix ~/alt ) LLVM-Clang trunk (23 apr 2012) successfully using GCC-4.6 on Ubuntu 12.04 and in turn libc++ using this Clang-build. When I want to use it I have to supply both -lc++ and -libstdc++ as follows /home/per/alt/bin/clang -x c++ -I/home/per/alt/include/v1 -L/home/per/alt/lib -std=gnu++0x -g -Wall ~/f.cpp -lm -lc++ -lstdc++ -lpthread -o f to compile f.cpp containing #include <iostream> using std::cout; using std::endl; int main(int argc, const char

Initializing a std::vector with default constructor

耗尽温柔 提交于 2019-12-22 07:01:25
问题 I have a class field which is a std::vector. I know how many elements I want this vector to contain: N. How do I initialize the vector with N elements? 回答1: std::vector has a constructor declared as: vector(size_type N, const T& x = T()); You can use it to construct the std::vector containing N copies of x . The default value for x is a value initialized T (if T is a class type with a default constructor then value initialization is default construction). It's straightforward to initialize a

Why is {} used to access operator() in std::hash?

安稳与你 提交于 2019-12-22 06:46:51
问题 While reading the examples of std::hash used for std::unordered_map, I noticed that the operator() function was being accessed by {}. http://en.cppreference.com/w/cpp/utility/hash result_type operator()(argument_type const& s) const { result_type const h1 ( std::hash<std::string>{}(s.first_name) ); result_type const h2 ( std::hash<std::string>{}(s.last_name) ); return h1 ^ (h2 << 1); // or use boost::hash_combine (see Discussion) } What does the use of {} here represent? 回答1: std::hash<T> is

Get POSIX epoch as system_clock::time_point

大城市里の小女人 提交于 2019-12-22 04:01:05
问题 I'm aware that the default value of a std::chrono::system_clock::time_point is the clock's epoch, but I can't find any mandate in the C++11 standard that system_clock 's epoch is the same as the POSIX epoch (1970-01-01T00:00:00Z). Is it safe to assume on Linux and Windows that this is the case? Or would it be smarter to use std::chrono::system_clock::from_time_t(0) ? 回答1: The standard leaves the epoch of std::chrono::system_clock::time_point unspecified. There are three implementations of std

Is the C++ Standard Library fully supported on Arduino?

拟墨画扇 提交于 2019-12-21 09:33:44
问题 Not asking about <string> but about the Standard-Library as a whole for use on micro-controllers. I don't yet own an Arduino board to execute code on, and as the title says, I'm curious if the C++ Standard-Library is fully supported on Arduino and already part of the Arduino IDE. It really goes without saying that the Standard-Library is probably the most efficient, fully tested, and resource-minimal publicly available C++ code out there, and would make life much easier to code for micro

Most efficient way to assign values to maps

旧巷老猫 提交于 2019-12-21 03:44:21
问题 Which way to assign values to a map is most efficient? Or are they all optimized to the same code (on most modern compilers)? // 1) Assignment using array index notation Foo["Bar"] = 12345; // 2) Assignment using member function insert() and STL pair Foo.insert(std::pair<string,int>("Bar", 12345)); // 3) Assignment using member function insert() and "value_type()" Foo.insert(map<string,int>::value_type("Bar", 12345)); // 4) Assignment using member function insert() and "make_pair()" Foo

What section of the C++ standard requires that set::erase calls destructors promptly

天大地大妈咪最大 提交于 2019-12-20 17:43:27
问题 What section of the C++11 standard (here's a copy of a draft standard) requires associative containers like std::set, std::map, std::unordered_set, and std::unordered_map to immediately call destructors of objects that are erased from them? To put it another way - are standard-compliant associative containers allowed to delay (not elide!) their calls to the key and/or value destructors of the keys and values they store? If not, what section in the standard forbids it? I ask because I am

Should reading negative into unsigned fail via std::cin (gcc, clang disagree)?

夙愿已清 提交于 2019-12-20 17:36:59
问题 For example, #include <iostream> int main() { unsigned n{}; std::cin >> n; std::cout << n << ' ' << (bool)std::cin << std::endl; } When input -1 , clang 6.0.0 outputs 0 0 while gcc 7.2.0 outputs 4294967295 1 . I'm wondering who is correct. Or maybe both are correct for the standard does not specify this? By fail, I take to mean (bool)std::cin be evaluated false. clang 6.0.0 fails input -0 too. As of Clang 9.0.0 and GCC 9.2.0, both compilers, using either libstdc++ or libc++ in the case of

Is std::call_once a blocking call?

南笙酒味 提交于 2019-12-20 03:15:06
问题 I'm using std::call_once in my code to initialize some shared variables only once. The calling code is inside a callback that is triggered by multiple threads. What I'm interested to know, since I couldn't find it in the documentation is whether std::call_once is blocking essentially as if there was a std::lock_guard instead? In practice it looks like this is the case. For example, the following will print "Done" before any print() will be called: #include <future> #include <iostream>

C++: string operator overload

时光总嘲笑我的痴心妄想 提交于 2019-12-20 02:55:09
问题 Can I overload existing function/operator in existing class? I was trying to do: #include <iostream> #include <string> using namespace std; string& string::operator<<(const string& str) { this->append(str); } But this gives me error: test.cpp:5: error: too few template-parameter-lists How can I do this? Or I can't? 回答1: You can't add member functions to a class unless you modify that class' definition. Use a free function instead: string& operator<<(string & lhs, const string & rhs) { return