c++17

Directly write into char* buffer of std::string

那年仲夏 提交于 2019-12-04 11:42:48
问题 So I have an std::string and have a function which takes char* and writes into it. Since std::string::c_str() and std::string::data() return const char* , I can't use them. So I was allocating a temporary buffer, calling a function with it and copying it into std::string . Now I plan to work with big amount of information and copying this buffer will have a noticeable impact and I want to avoid it. Some people suggested to use &str.front() or &str[0] but does it invoke the undefined behavior?

template type deduction failing (std::empty as a predicate)

梦想的初衷 提交于 2019-12-04 11:18:12
I have a vector of vectors, and I want to check if all of them are empty. Using the standard library, I tried: #include <algorithm> #include <vector> int main() { std::vector<std::vector<int>> vv; std::all_of(std::begin(vv), std::end(vv), std::empty); } This result in the following error in clang 7.0: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/bits/stl_algo.h:508:5: note: candidate template ignored: couldn't infer template argument '_Predicate' I guess it's a standard behavior, due to the rules of type deduction. But anyway, what i the easiest way to workaround

How does the C++17 proposal for uniform call syntax intend to handle namespaces?

百般思念 提交于 2019-12-04 10:31:12
问题 As far as I know, there are two propsals for uniform call syntax for C++17 (where the other one is called unified call syntax). Reading them, I cant see how they intend to handle namespaces. Example: class Class {...} namespace MyNamespace { void f(Class x, Class y); } Will it be possible to call this method using something like: Class a, b; a.MyNamespace::f(b); Or do both the free function, and the class need to be defined in the same namespace? References : http://www.open-std.org/jtc1/sc22

Is copy/move elision allowed to make a program using deleted functions well-formed?

 ̄綄美尐妖づ 提交于 2019-12-04 10:29:49
Consider the following code: #include <iostream> struct Thing { Thing(void) {std::cout << __PRETTY_FUNCTION__ << std::endl;} Thing(Thing const &) = delete; Thing(Thing &&) = delete; Thing & operator =(Thing const &) = delete; Thing & operator =(Thing &&) = delete; }; int main() { Thing thing{Thing{}}; } I expect Thing thing{Thing{}}; statement to mean construction of temporary object of Thing class using default constructor and construction of thing object of Thing class using move constructor with just created temporary object as an argument. And I expect that this program to be considered

Is it possible to create a lambda on the heap in one step? [duplicate]

依然范特西╮ 提交于 2019-12-04 09:58:07
问题 This question already has answers here : Lambda with dynamic storage duration (4 answers) Closed last year . We can create a lambda like this: auto x = [](){}; I can create a copy of this on the heap like this: auto y = new decltype(x)(x); The question is, is it possible to do this in one step? Creating a lambda on the heap without extra steps? 回答1: You can use auto in a new-expression: new auto ([](){}); 来源: https://stackoverflow.com/questions/51839698/is-it-possible-to-create-a-lambda-on

Shall structured binding to a copy of a const c-array be const?

我怕爱的太早我们不能终老 提交于 2019-12-04 09:57:32
问题 Consider this code (demo): #include <tuple> #include <type_traits> struct Ag{int i;int j;}; using T = std::tuple<int,int>; using Ar = int[2]; const Ag ag {}; const T t {}; const Ar ar {}; void bind_ag(){ auto [i,j] = ag; static_assert(std::is_same_v<decltype((i)),int&>); } void bind_t(){ auto [i,j] = t; static_assert(std::is_same_v<decltype((i)),int&>); } void bind_ar(){ auto [i,j] = ar; static_assert(std::is_same_v<decltype((i)),int&>); //For GCC static_assert(std::is_same_v<decltype((i))

Is std::move safe in an arguments list when the argument is forwarded, not move constructed?

百般思念 提交于 2019-12-04 09:18:58
Trying to provide a solution to std::string_view and std::string in std::unordered_set , I'm playing around with replacing std::unordered_set<std::string> with std::unordered_map<std::string_view, std::unique_ptr<std::string>> (the value is std::unique_ptr<std::string> because the small string optimization would mean that the address of the string 's underlying data will not always be transferred as a result of std::move . My original test code, that seems to work, is (omitting headers): using namespace std::literals; int main(int argc, char **argv) { std::unordered_map<std::string_view, std:

Why doesn't this “undefined extern variable” result in a linker error in C++17?

对着背影说爱祢 提交于 2019-12-04 08:47:57
问题 I have compiled and ran the following program in a C++17 compiler (Coliru). In the program, I declared an extern variable, but did not define it. However, the compiler doesn't give a linker error . #include <iostream> extern int i; // Only declaration int func() { if constexpr (true) return 0; else if (i) return i; else return -1; } int main() { int ret = func(); std::cout<<"Ret : "<<ret<<std::endl; } Why doesn't the compiler give a linker error? 回答1: Because the variable isn't odr-used. You

How to properly access mapped memory without undefined behavior in C++

牧云@^-^@ 提交于 2019-12-04 08:30:05
问题 I've been trying to figure out how to access a mapped buffer from C++17 without invoking undefined behavior. For this example, I'll use a buffer returned by Vulkan's vkMapMemory. So, according to N4659 (the final C++17 working draft), section [intro.object] (emphasis added): The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is created by a definition (6.1), by a new-expression (8.3.4), when implicitly changing the active member of a union (12

Why is std::shared_ptr::unique() deprecated?

时光毁灭记忆、已成空白 提交于 2019-12-04 08:25:41
问题 What is the technical problem with std::shared_ptr::unique() that is the reason for its deprecation in C++17? According to cppreference.com, std::shared_ptr::unique() is deprecated in C++17 as this function is deprecated as of C++17 because use_count is only an approximation in multi-threaded environment. I understand this to be true for use_count() > 1 : While I'm holding a reference to it, someone else might simultaneously let go of his or create a new copy. But if use_count() returns 1