c++14

Passing const unique_ptr reference as parameter

China☆狼群 提交于 2020-01-04 05:51:09
问题 void f1(unique_ptr<A[]>& upA){ //some work... //callee can mess up smart pointer many ways for caller upA.reset(); //some work... } void f2(const unique_ptr<A[]>& upA){ //some work... //compiler stops callee from ruining smart pointer many ways for caller upA.reset(); //some work... } f1(upAArray); f2(upAArray); In the above code, calling f1 is dangerous because the callee can mess up the smart pointer by resetting it, releasing it, etc. Calling f2 is safe and everything works great. If

How to pass template function with default arguments to std::call_once

元气小坏坏 提交于 2020-01-04 03:58:26
问题 I need to use std::call_once in my templatized singleton class but currently below sample code is not compiling : std::once_flag flag; class LifeTrackerHelper { public: template<class T> inline static int SetLongevity(std::unique_ptr<T>& pobj,unsigned int longevity = 0) { return 0; } }; template<class T> class Singleton { public: inline static T* getInstance() { static std::unique_ptr<T> ptr(new T()); std::call_once(flag,&LifeTrackerHelper::SetLongevity<T>,ptr); //static int i =

C++11/14 and return( … ) vs return

给你一囗甜甜゛ 提交于 2020-01-04 03:47:29
问题 In C++ you are allowed to write a return statement that looks like : return ( ... ); which is different from the more popular : return ... ; In particular the first version returns the address/reference of something that is local to the stack of the function which contains that return statement. Now why something would like to return a reference to something that, at that point, has no lifetime ? What are the use case for this idiom ? Considering the new buzzword and features from C++11 and C

C++11/14 and return( … ) vs return

社会主义新天地 提交于 2020-01-04 03:47:02
问题 In C++ you are allowed to write a return statement that looks like : return ( ... ); which is different from the more popular : return ... ; In particular the first version returns the address/reference of something that is local to the stack of the function which contains that return statement. Now why something would like to return a reference to something that, at that point, has no lifetime ? What are the use case for this idiom ? Considering the new buzzword and features from C++11 and C

Moving a unique_lock<recursive_mutex> to another thread

核能气质少年 提交于 2020-01-03 21:01:30
问题 I was wondering what happens when you move a unique_lock that holds a recursive_mutex . Specifically, I was looking at this code: recursive_mutex g_mutex; #define TRACE(msg) trace(__FUNCTION__, msg) void trace(const char* function, const char* message) { cout << std::this_thread::get_id() << "\t" << function << "\t" << message << endl; } future<void> foo() { unique_lock<recursive_mutex> lock(g_mutex); TRACE("Owns lock"); auto f = std::async(launch::async, [lock = move(lock)]{ TRACE("Entry");

Differences between c++14 std::experimental::filesystem::v1 and c++17 std::filesystem?

守給你的承諾、 提交于 2020-01-03 15:35:23
问题 I found this page, describing the changes between c++14 and c++17: https://isocpp.org/files/papers/p0636r0.html ... It links to this page, which describes the proposed filesystem changes: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0218r0.html I skimmed through it. There are small wording changes to the standard, but the only code change I saw were namespace changes that removed the "experimental" and "v1" parts, so "std::experimental::filesystem::v1" became "std::filesystem",

Support of std::cbegin() in C++14

雨燕双飞 提交于 2020-01-03 13:35:45
问题 Item 13 from Scott Mayers' "Effective Modern C++" states to prefer const_iterators over iterators. I agree but I also want to use non-member functions rather than member functions. According to the book there should be a non-member function std::cbegin() and std::cend() in C++14. To make use of this functions I just installed gcc version 4.9.2 and compiled with the flag -std=c++14 . It seems to compile until I try to use std::cbegin() . I start searching for the support for this function but

std::is_constructible doesn't give the correct result [duplicate]

混江龙づ霸主 提交于 2020-01-03 09:03:12
问题 This question already has answers here : Why does is_constructible claim something is constructible when it isn't? (2 answers) Closed 3 years ago . Originated from this CodeReview topic: #include <cstddef> #include <algorithm> #include <iostream> #include <type_traits> #include <utility> template <typename T> class aggregate_wrapper : public T { private: using base = T; public: using aggregate_type = T; template <typename... Ts> aggregate_wrapper(Ts&&... xs) : base{std::forward<Ts>(xs)...} {

std::is_constructible doesn't give the correct result [duplicate]

时光毁灭记忆、已成空白 提交于 2020-01-03 09:02:15
问题 This question already has answers here : Why does is_constructible claim something is constructible when it isn't? (2 answers) Closed 3 years ago . Originated from this CodeReview topic: #include <cstddef> #include <algorithm> #include <iostream> #include <type_traits> #include <utility> template <typename T> class aggregate_wrapper : public T { private: using base = T; public: using aggregate_type = T; template <typename... Ts> aggregate_wrapper(Ts&&... xs) : base{std::forward<Ts>(xs)...} {

C++ conversion operator to chrono::duration - works with c++17 but not C++14 or less

微笑、不失礼 提交于 2020-01-03 08:35:30
问题 The following code compiles with gcc 7.1.0 with C++17 set but does not compile with C++14 set (or Visual Studio 2017). It is easy to reproduce on Wandbox. What has to be done to make it work with C++11/14? #include <iostream> #include <chrono> int main() { struct Convert { operator std::chrono::milliseconds() { std::cout << "operator std::chrono::milliseconds" << std::endl; return std::chrono::milliseconds(10); } operator int64_t () { std::cout << "operator int64_t" << std::endl; return 5; }