c++-standard-library

Comparison for objects derived from std::string_view is ambiguous in MSVC

旧巷老猫 提交于 2019-12-05 07:50:46
TL;DR: Can I expect that the code below will compile on any c++17 conformant c++ toolchain (based on the current c++17 proposal) and the failure of MSVC to do so is a bug in their implementation? #include <string_view> struct Foo : std::string_view {}; int main() { Foo f1{}; Foo f2{}; return f1 == f2; } Explanation: I have a class that is derived from std::string_view and doesn't implement its own comparison operators, because the std::string_view semantics are exactly what I need and I also want it to be comparable to e.g. a std::string . However, if I try to compare two instances of that

Get POSIX epoch as system_clock::time_point

喜夏-厌秋 提交于 2019-12-05 02:37:32
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) ? The standard leaves the epoch of std::chrono::system_clock::time_point unspecified. There are three implementations of std::chrono::system_clock::time_point I am aware of: libc++ libstdc++ VS All three of these are thin wrappers

How to Fix Visual Studio 2012 error LNK2019: unresolved external symbol \"__declspec(dllimport) public: class std::basic_string?

十年热恋 提交于 2019-12-05 01:15:21
How to fix a Visual Studio 2012 error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_string? I've been compiling a solution containing one exe and several static lib projects on which the exe depends fine with Visual Studio 2008. The static libs include: TCL: tcl84tsx.lib wxWidgets: wxbase.lib zlib.lib ws2_32.lib xerces-c_2.lib SNMP Research EMANATE: subagent.lib,agent.lib,emanate.lib,pack.lib,mibtable.lib,devkit.lib etc. I loaded the solution into Visual Studio 2012 and have compiled all the projects in the solution but when I try to link the exe, I'm

What is wrong with my usage of C++ standard library's find?

柔情痞子 提交于 2019-12-04 23:11:30
I'm trying to use the C++ standard library's find algorithm like this: template<class T> const unsigned int AdjacencyList<T>::_index_for_node( const std::vector<T>& list, const T& node ) throw(NoSuchNodeException) { std::vector<T>::iterator iter = std::find(list.begin(), list.end(), node); } When I try to compile, I get these errors: In file included from ../AdjacencyList.cpp:8: ../AdjacencyList.h: In member function ‘const unsigned int Graph::AdjacencyList<T>::_index_for_node(const std::vector<T, std::allocator<_Tp1> >&, const T&)’: ../AdjacencyList.h:99: error: expected ‘;’ before ‘iter’ ..

What is the difference between shuffle and random_shuffle c++

喜欢而已 提交于 2019-12-04 16:31:09
问题 The function std::shuffle has been introduced in C++11: template< class RandomIt, class URNG > void shuffle( RandomIt first, RandomIt last, URNG&& g ); and it has the same signature as one of the overloads of std::random_shuffle which was also introduced in C++11: template< class RandomIt, class RandomFunc > void random_shuffle( RandomIt first, RandomIt last, RandomFunc&& r ); The difference is in the third parameter where: URNG must meet the requirements of UniformRandomNumberGenerator Is

Why does the standard C++ library use all lower case?

时间秒杀一切 提交于 2019-12-04 15:25:51
问题 Just curious why the C++ standard library uses all lower case and underscores instead of camelCase or PascalCase naming convention. Personally, I find the latter much easier to deal with when typing out code, but is there some kind of legitimate reason to use the former? 回答1: Main reason : To keep compatibility with the existing code, since they have done it with C also. Also have a look at these C++ Coding standards, which presents some generic reasoning regarding the importance of

Does redefining a function from the standard library violate the one-definition rule?

不羁的心 提交于 2019-12-04 10:59:33
#include <cmath> double log(double) {return 1.0;} int main() { log(1.0); } Suppose the function log() in <cmath> is declared in global namespace (this is unspecified in fact, and we just make this assumption), then it refers to the same function as the log() function we defined. So does this code violate the one-definition rule (see here , since no diagnostic required, this code may compile in some compiler and we cannot assert if it is correct)? Note : After recent edits, this is not a duplicate of: What exactly is One Definition Rule in C++? Typical scenario. If extern "C" double log(double)

different compare signature for std::upper_bound and std::lower_bound

空扰寡人 提交于 2019-12-04 06:29:06
Here is a sample example for both std::lower_bound & std::upper_bound , notice the signature of Compare lambda being passed to them - const auto lower_x = std::lower_bound( points.begin(), points.end(), rec.min_corner.x, [](const RankedPoint &rp, const double x) { return rp.point.x < x; }); const auto upper_x = std::upper_bound( points.begin(), points.end(), rec.max_corner.x, [](const double x, const RankedPoint &rp) { return x < rp.point.x; }); What is the possible reasoning behind keeping the signature exact opposite of each other? I wasn't aware of this and gcc compiled (clang didn't) it

Why aren't std::count and std::find optimised to use memchr?

陌路散爱 提交于 2019-12-04 06:00:39
I was reading sehe's answer to this question and was surprised to see sehe found using a hand written loop using std::memchr to be over 3 times faster than using std::count (see comments). The code using std::count can be seen in edit 2, but it basically boils down to: const auto num_lines = std::count(f, l, '\n'); vs uintmax_t num_lines = 0; while (f && f != l) if ((f = static_cast<const char*>(memchr(f, '\n', l - f)))) num_lines++, f++; I would have expected the std::count version to be at least as fast as the std::memchr one - for a similar reason to why using std::copy should be at least

Are C++11 containers supported by Cython?

落爺英雄遲暮 提交于 2019-12-04 04:22:28
Cython gives us an easy way to import C++ standard library data structures, e.g.: from libcpp.vector cimport vector from libcpp.utility cimport pair But what about newer containers introduced with C++11: std::unordered_map , std::unordered_set etc. Are they supported in the same way? I could not find the appropriate import statement. Cython doesn't support them by default, but you could probably create your own interface, following the structure of https://github.com/cython/cython/blob/master/Cython/Includes/libcpp/map.pxd . Cython now supported unordered_map and unordered_set since 0.20.2 .