c++14

Why do we need the two definitions: integral constant expression and converted constant expression?

非 Y 不嫁゛ 提交于 2019-12-03 08:46:31
问题 §5.19/3 in C++14 defines an integral constant expression and a converted constant expression: An integral constant expression is an expression of integral or unscoped enumeration type, implicitly converted to a prvalue, where the converted expression is a core constant expression. [ Note: Such expressions may be used as array bounds (8.3.4, 5.3.4), as bit-field lengths (9.6), as enumerator initializers if the underlying type is not fixed (7.2), and as alignments (7.6.2). —end note ] A

Networking Library in C++14

♀尐吖头ヾ 提交于 2019-12-03 08:40:22
问题 Herb Sutter writes here (on his ISO C++ Spring 2013 meeting trip report) that a networking library is planned to be added to C++14. What features would this library have initially? What is it based on? Is there a proof-of-concept implementation? My Google-fu must be seriously lacking, because I can't even find the proposal draft. There are a series of blog posts on http://meetingcpp.com/ listing the proposals for C++14: part 1, part 2, part 3 and part 4. Among all these, I can only find two

Pass parameters to std::thread wrapper

女生的网名这么多〃 提交于 2019-12-03 08:32:44
I want to implement a small thread wrapper that provides the information if a thread is still active, or if the thread has finished its work. For this I need to pass the function and its parameters to be executed by the thread class to another function. I have a simple implementation that should work but cannot get it to compile, and I can't figure out what to do to make it work. Here is my code: #include <unistd.h> #include <iomanip> #include <iostream> #include <thread> #include <utility> class ManagedThread { public: template< class Function, class... Args> explicit ManagedThread( Function&

Directly write into char* buffer of std::string

久未见 提交于 2019-12-03 08:16:41
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? C++98/03 Impossible. String can be copy on write so it needs to handle all reads and writes. C++11/14

Can we use the return value optimization when possible and fall back on move, not copy, semantics when not?

我怕爱的太早我们不能终老 提交于 2019-12-03 08:11:55
Is it possible to write C++ code where we rely on the return value optimization (RVO) when possible, but fall back on move semantics when not? For example, the following code can not use the RVO due to the conditional, so it copies the result back: #include <iostream> struct Foo { Foo() { std::cout << "constructor" << std::endl; } Foo(Foo && x) { std::cout << "move" << std::endl; } Foo(Foo const & x) { std::cout << "copy" << std::endl; } ~Foo() { std::cout << "destructor" << std::endl; } }; Foo f(bool b) { Foo x; Foo y; return b ? x : y; } int main() { Foo x(f(true)); std::cout << "fin" << std

Type traits to check if class has member function

白昼怎懂夜的黑 提交于 2019-12-03 07:07:45
Trying to create a way to identify if a given class has a given function that can be invoked, and returns some type. Any idea on what I'm doing wrong here? Is there a better way to determine if a given method is invoke'able given a class? #include <string> #include <type_traits> #define GENERATE_HAS_MEMBER_FUNC(func, rettype) \ template<typename T, class Enable = void> struct has_##func; \ template<typename T, class U> struct has_##func : std::false_type {}; \ template<typename T> \ struct has_##func<T, \ typename std::enable_if<std::is_same< \ typename std::result_of<decltype (&T::func)(T)>:

clang error with std::unique_ptr

江枫思渺然 提交于 2019-12-03 06:55:35
I have base object called IList . Then I have VectorList , that inherits IList . then I have function like this: std::unique_ptr<IList> factory(){ auto vlist = std::make_unique<VectorList>(); return vlist; } This compiles without problem under gcc , but clang gives following error: test_file.cc:26:9: error: no viable conversion from 'unique_ptr<VectorList, default_delete<VectorList>>' to 'unique_ptr<IList, default_delete<IList>>' return vlist; How is proper way to handle this kind of errors? It appears (your version of) Clang is still following C++11 behaviour in this regard. In C++11, you had

Generic lambda argument for std::pair

拈花ヽ惹草 提交于 2019-12-03 06:51:00
I'm trying to see if this is possible in the C++14 generic lambda, but I cannot find a right way to express it (or perhaps it is not possible). The simplified example is: auto confirmOperation = [](auto pr){ assert(pr.second); }; The idea is that if you pass it an std::pair where the second is a bool (such as what is returned from emplace functions), it can look at this bool. If this was a template parameter instead, I could explicitly show the pair with the types of the pair as generic, but I don't think that is possible with a lambda? Thus instead I mark the entire argument as generic, and

Multiple Variadic Parameter Pack for Template Class

独自空忆成欢 提交于 2019-12-03 06:45:38
I am using variadic parameter packs for policy based class design. template <APITypes APIType, class... Policies> class IShader : public Policies... { }; Policies are defined when called or with defaults if none are specified. The problem comes when I need to add another variadic parameter pack: template <AttributeType... Attributes, APITypes APIType, class... Policies> class IShader : public Policies... { }; This results in the error "Template parameter pack must be the last template parameter". I am planning to use the attribute pack to change the behaviour of at least one of the policies.

Capture and move a unique_ptr in a c++14 lambda expression

跟風遠走 提交于 2019-12-03 06:38:22
问题 I am capturing a unique_ptr in a lambda expression this way: auto str = make_unique<string>("my string"); auto lambda = [ capturedStr = std::move(str) ] { cout << *capturedStr.get() << endl; }; lambda(); It works great until I try to move capturedStr to another unique_ptr. For instance, the following is not working: auto str = make_unique<string>("my string"); auto lambda = [ capturedStr = std::move(str) ] { cout << *capturedStr.get() << endl; auto str2 = std::move(capturedStr); // <--- Not