c++14

What is the lifetime of the arguments of std::async?

感情迁移 提交于 2019-12-10 14:55:29
问题 It appears that arguments of a function executed via std::async share the lifetime of the future: #include <iostream> #include <future> #include <thread> struct S { S() { std::cout << "S() " << (uintptr_t)this << std::endl; } S(S&& s) { std::cout << "S(&&) " << (uintptr_t)this << std::endl; } S(const S& s) = delete; ~S() { std::cout << "~S() " << (uintptr_t)this << std::endl; } }; int main() { { std::cout << "enter scope" << std::endl; auto func = [](S&& s) { std::cout << "func " << (uintptr

Function parameter type using decltype

浪尽此生 提交于 2019-12-10 14:39:38
问题 Note: the example provided in this question is not production code and has no sense at all. It is just there to illustrate my problem. I was testing the possibilities of decltype , especially if it is used to deduce function parameter types, and ran into a problem: Suppose there were two classes structured like this: struct ClassInt { // Note: no default ctor ClassInt(int value) : m_Value(value) {} int m_Value; }; struct ClassDouble { // Note: no default ctor ClassDouble(double value) : m

Visual Studio 2015 build error with Clang 3.7

北慕城南 提交于 2019-12-10 14:33:52
问题 So this is a followup question to Visual Studio 2015 Update 1, clang error The error message I'm getting is clang.exe : error : cannot specify -o when generating multiple output files Basically, Hans Passant's workaround of disabling precompiled headers did not work for me. I'm still seeing the error. Anybody have any more ideas to work around this? My VS-generated command line is: -fpic -std=c++1y -fstack-protector -x c++ "Debug\" -Wall -fno-strict-aliasing -ffunction-sections -I "c:\SDKs

C++11/14 INVOKE workaround

孤街醉人 提交于 2019-12-10 14:25:41
问题 I need to use the INVOKE semantics (implemented by std::invoke in C++17) in some C++11/14 code. I certainly don't want to implement it myself, which I believe would be a disaster. So I decided to make use of present standard library facilities. Quickly came to my mind was: template<typename Fn, typename... Args> constexpr decltype(auto) my_invoke(Fn&& f, Args&&... args) noexcept(noexcept(std::bind(std::forward<Fn>(f), std::forward<Args>(args)...)())) { return std::bind(std::forward<Fn>(f),

C++ : Check if the template type is one of the variadic template types [duplicate]

风格不统一 提交于 2019-12-10 14:23:07
问题 This question already has answers here : Check if a type is passed in variadic template parameter pack (2 answers) Closed 4 years ago . Let's say we have function: template <typename Kind, typename... Kinds> void foo(){...}; What is the simplest way to check if the type 'Kind' is one of the types 'Kinds' in C++ (including C++1z)? 回答1: You could use the following type trait: template <typename...> struct is_one_of { static constexpr bool value = false; }; template <typename F, typename S,

What C++14 rule prohibits constexpr functions from making assignments to data members?

三世轮回 提交于 2019-12-10 14:12:12
问题 My understanding is that this (nonsensical) code is not valid C++14: class Point { public: constexpr double setX(double newX) { return x = newX; } private: double x; }; I'm trying to figure out what part of the (still officially draft) C++14 Standard disallows it. The restrictions on constexpr functions are listed in 7.1.5/2. (Sorry for the mangled formatting. I can't figure out how to beat markdown into making it look right.) The definition of a constexpr function shall satisfy the following

How to pass vector elements as arguments to variadic template function?

本小妞迷上赌 提交于 2019-12-10 13:38:48
问题 So, let's say I have this code: template <class T1, class T2> auto sum(T1 a, T2 b) ->decltype(a + b) { return a + b; } template <class T1, class T2, class... T3> auto sum(T1 a, T2 b, T3... tail) ->decltype(a + sum(b, tail...)) { return a + sum(b, tail...); } I would like to call function sum in a way I pass a vector: vector<double> numbers = { 1, 2, 6, 5 }; that should be used as a list of arguments for function sum . How can I do that? Calling function sum should return 14 in this case. 回答1:

How to insert an integer with leading zeros into a std::string?

点点圈 提交于 2019-12-10 13:37:01
问题 In a C++14 program, I am given a string like std::string s = "MyFile####.mp4"; and an integer 0 to a few hundred. (It'll never be a thousand or more, but four digits just in case.) I want to replace the " #### " with the integer value, with leading zeros as needed to match the number of '#' characters. What is the slick C++11/14 way to modify s or produce a new string like that? Normally I would use char* strings and snprintf() , strchr() to find the " # ", but figure I should get with modern

Const casting empty base class

我的未来我决定 提交于 2019-12-10 13:36:41
问题 Is it undefined behavior to const_cast away an empty base class and call a non const method on it? For example class EmptyBase { public: void bar() { ... } }; class Something : public EmptyBase { public: void foo() const { const_cast<EmptyBase&>(static_cast<const EmptyBase&>(*this)).bar(); } }; I haven't been able to find relevant information in the standards (C++14 and C++17) that answers this.. 回答1: It's not UB in and of itself. You get undefined behavior when you cast away constness and

Clang fails to compile template function in a template class specialization, which has *distinct return type* from the template declaration

为君一笑 提交于 2019-12-10 13:22:25
问题 The following function derefItemX() is compiled fine on GCC 4.8-5.3, but fails on CLang 3.8: //! Accessory Operations - template argument depended wrappers template<bool SIMPLE> // For Nodes / non-scoped storage struct Operations { //! \brief Defererence wrapped or direct iterator //! //! \param iel IItemXT& - iterator to be dereferenced //! \return ItemT& - resulting reference template<typename IItemXT> constexpr static auto& derefItemX(IItemXT& iel) { static_assert(is_base_of<std::forward