c++17

Why does the 32769th insert fail in std::unordered_set?

泪湿孤枕 提交于 2021-01-27 15:06:34
问题 I generate a large number of class instances and store them in a std::unordered_set . I have defined a hash function and an equality relation, and so far everything works as it should - I insert 10000 instances with unordered_set::insert , and I can find them with unordered_set::find . All the objects are undamaged, and there is no hint on memory corruption or any other issue. However, when I keep inserting, the 32769th insert fails - it doesn't throw, but it returns a pair where the iterator

Assign variant<A,B,C> from variant<C,B>?

天涯浪子 提交于 2021-01-27 14:51:33
问题 Using = does not work. I have code like this, but it is a "bit" ugly. #include <iostream> #include <cassert> #include <variant> #include <string> using namespace std; namespace detail { template<typename... L, typename... R> void VariantAssignRec(variant<L...>* lhs, const variant<R...>&rhs, size_t rhs_idx, std::integral_constant<int, -1>) { } template<typename... L, typename... R, int get_idx> void VariantAssignRec(variant<L...>* lhs, const variant<R...>&rhs, size_t rhs_idx, std::integral

std::vector<std::wstring> Is moving/reallocating inner wstring.data() legal?

夙愿已清 提交于 2021-01-27 13:31:40
问题 Here is an excerpt: ... std::vector<std::wstring> vecWstr; vecWstr.emplace_back(L"1"); wchar_t* data1 = vecWstr[0].data(); //<-This pointer needed for future use. vecWstr.emplace_back(L"2"); wchar_t* data2 = vecWstr[0].data(); if (data1 != data2) MessageBox(L"Error, not equal.", L"Compare"); MessageBox always arises. So, here i compare two wstring buffers before and after .emplace() . In my understanding they must be equal. The main concern here is: Why vector moves/reallocates 1st innner std

std::vector<std::wstring> Is moving/reallocating inner wstring.data() legal?

不打扰是莪最后的温柔 提交于 2021-01-27 13:26:06
问题 Here is an excerpt: ... std::vector<std::wstring> vecWstr; vecWstr.emplace_back(L"1"); wchar_t* data1 = vecWstr[0].data(); //<-This pointer needed for future use. vecWstr.emplace_back(L"2"); wchar_t* data2 = vecWstr[0].data(); if (data1 != data2) MessageBox(L"Error, not equal.", L"Compare"); MessageBox always arises. So, here i compare two wstring buffers before and after .emplace() . In my understanding they must be equal. The main concern here is: Why vector moves/reallocates 1st innner std

No member named 'size' in namespace 'std'

僤鯓⒐⒋嵵緔 提交于 2021-01-27 13:16:27
问题 I'm trying to port some C++ code from Windows to OS X (using Xcode). The following code: writePosition %= std::size(bufferL); is generating an error: No member named 'size' in namespace 'std' How do I fix this? 回答1: std::size() is available starting from C++17. Try enabling -std=c++17 for your compiler. Also, double check that the source files contain #include <iterator> , either directly, or indirectly by #include 'ing any of the following headers: <array> <deque> <forward_list> <list> <map>

Can a constexpr c-string used as template parameter be instantiated in a template function?

南楼画角 提交于 2021-01-27 12:49:16
问题 I have been prototyping a library in which I would like to use a c-string value defined locally in a function as a non-type template parameter. In essence, the bare minimum implementation is the following (see on godbolt): #include <cstdio> #include <type_traits> template <char const* C> class compile_string {}; template <typename T> auto foo() { static constexpr char const CSTR[] = "Hello, World!"; return compile_string<CSTR>{}; } int main() { auto shorty = foo<short>(); auto inty = foo<int>

Are inline static variables unique across modules in visual c++?

孤街醉人 提交于 2021-01-27 12:23:25
问题 c++17 introduce inline (static) variables. It is said that "The compiler will guarantee that a variable has only one definition and it’s initialised only once through all compilation units." I am wondering if visual c++ guarantee inline static variable will be unique across multiple modules (dlls and exe). //cat.h class __declspec(dllexport) Cat { public: inline static int var = 0; }; If cat.h is included in multiple dlls and one exe, is Cat::var unique in the application ? 回答1: Your question

why cout is not printing this string?

蓝咒 提交于 2021-01-27 11:50:55
问题 So, I'm new to C++ and I can't figure why this is happening. I have a string with all the alphabets and I copied 10 characters from it into a new string s2 , character by character using a for loop as follows and when I execute it the cout function is printing a blank line. #include <iostream> using namespace std; int main(){ string s = "abcdefghijklmnopqrstuvwxyz"; string s2; for(int i=0; i<10; i++){ s2[i] = s[i]; } cout << s2 << endl; return 0; } But when I print this string s2 character by

why cout is not printing this string?

﹥>﹥吖頭↗ 提交于 2021-01-27 11:45:13
问题 So, I'm new to C++ and I can't figure why this is happening. I have a string with all the alphabets and I copied 10 characters from it into a new string s2 , character by character using a for loop as follows and when I execute it the cout function is printing a blank line. #include <iostream> using namespace std; int main(){ string s = "abcdefghijklmnopqrstuvwxyz"; string s2; for(int i=0; i<10; i++){ s2[i] = s[i]; } cout << s2 << endl; return 0; } But when I print this string s2 character by

Combining n vectors into one vector of n-tuples

南笙酒味 提交于 2021-01-27 11:44:35
问题 I'm thinking about a function with signature template<typename ...Ts> std::vector<std::tuple<Ts...>> join_vectors(std::vector<Ts>&&...) { //... }; but probably a more general one accepting any iterable instead of just std::vector would be good. Probably it would have a signature like this? template<template<typename> typename C, typename ...Ts> C<std::tuple<Ts...>> join_vectors(C<Ts>&&...) { // ... }; However, I'm not at this level yet in C++ (despite doing the same in Haskell would be