c++14

Is it possible to figure out the parameter type and return type of a polymorphic C++ 14 lambda?

我是研究僧i 提交于 2020-01-12 08:08:53
问题 Starting from this question (Is it possible to figure out the parameter type and return type of a lambda?) I used the proposed function_traits a lot. However, with C++14 polymorphic lambdas have arrived and they gave me a hard time. template <typename T> struct function_traits : public function_traits<decltype(&T::operator())> {}; // For generic types, directly use the result of the signature of its 'operator()' template <typename ClassType, typename ReturnType, typename... Args> struct

Integer sequence of chars from user-defined literal taking strings as parameters

梦想的初衷 提交于 2020-01-12 07:59:06
问题 Currently, only doubles can produce a template of chars in a user defined literal: template <char...> double operator "" _x(); // Later 1.3_x; // OK "1.3"_y; // C++14 does not allow a _y user- // defined operator to parse that as a template of chars Is there a clever way to produce a std::integer_sequence of chars using a user defined literal. In other words, what the code of _y(const char*, std::size_t) would be so that I end up with a std::integer_sequence<char, '1', '.', '3'> ? 回答1: At

Abbreviated function template vs. function template with forwarding reference param

家住魔仙堡 提交于 2020-01-12 07:30:33
问题 What are the differences between function templates with forwarding reference parameters template<typename T> void Universal_func(T && a) { } and abbreviated function templates ? void auto_fun(auto && a) { } Can I replace Universal_func with auto_fun ? Is Universal_func a of auto_fun or are they equal? I have tested the below program. It seems that both are the same. template<typename T> void Universal_func(T && a) { } void auto_fun(auto && a) { } int main() { int i; const int const_i = 0;

Why are mutexes and condition variables trivially copyable?

大憨熊 提交于 2020-01-12 04:54:07
问题 LWG 2424 discusses the undesirable status of atomics, mutexes and condition variables as trivially copyable in C++14. I appreciate that a fix is already lined up, but std::mutex, std::condition variable et al. appear to have non-trivial destructors. For example: 30.4.1.2.1 Class mutex [thread.mutex.class] namespace std { class mutex { public: constexpr mutex() noexcept; ~mutex(); // user-provided => non-trivial … } } Shouldn't this disqualify them as trivially copyable? 回答1: Either it was my

Local static/thread_local variables of inline functions?

只愿长相守 提交于 2020-01-12 04:41:07
问题 If I have a static local variable or thread_local local variable that is within an inline function that is defined in different translation units, in the final program are they guaranteed by the standard to have the same address? // TU1: inline int* f() { static int x; return &x; } extern int* a; void sa() { a = f(); } // TU2: inline int* f() { static int x; return &x; } extern int* b; void sb() { b = f(); } // TU3: int *a, *b; void sa(); void sb(); int main() { sa(); sb(); return a == b; }

How can this code be constexpr? (std::chrono)

夙愿已清 提交于 2020-01-11 08:29:27
问题 In the standards paper P0092R1, Howard Hinnant wrote: template <class To, class Rep, class Period, class = enable_if_t<detail::is_duration<To>{}>> constexpr To floor(const duration<Rep, Period>& d) { To t = duration_cast<To>(d); if (t > d) --t; return t; } How can this code work? The problem is that operator-- on a std::chrono::duration is not a constexpr operation. It is defined as: duration& operator--(); And yet this code compiles, and gives the right answer at compile time: static_assert

function-try-block and noexcept

假装没事ソ 提交于 2020-01-11 08:25:08
问题 For the following code struct X { int x; X() noexcept try : x(0) { } catch(...) { } }; Visual studio 14 CTP issues the warning warning C4297: 'X::X': function assumed not to throw an exception but does note: __declspec(nothrow), throw(), noexcept(true), or noexcept was specified on the function Is this a misuse of noexcept ? Or is it a bug in Microsoft compiler? 回答1: Or is it a bug in Microsoft compiler? Not quite. A so-called function-try-block like this cannot prevent that an exception will

Create multiple indexes into a large collection of objects with smart pointers

风格不统一 提交于 2020-01-11 07:51:09
问题 I am creating multiple indexes (ie, that use different keys) into a large collection of objects. The objects can change, and the collection can shrink and grow. My thoughts so far: Keep multiple collections of some kind of pointers to the objects. Use set instead of map for better encapsulation. Use unordered_set to scale well with large data sets. The pointers should ideally all be some form of smart pointer. I can start off fairly easily with a master collection of unique_ptrs, which manage

Deleted Function in std::pair when using a unique_ptr inside a map

淺唱寂寞╮ 提交于 2020-01-11 04:44:05
问题 I have a piece of C++ code for which I am not sure whether it is correct or not. Consider the following code. #include <memory> #include <vector> #include <map> using namespace std; int main(int argc, char* argv[]) { vector<map<int, unique_ptr<int>>> v; v.resize(5); return EXIT_SUCCESS; } The GCC compiles this code without a problem. The Intel compiler (version 19), however, stops with an error: /usr/local/ [...] /include/c++/7.3.0/ext/new_allocator.h(136): error: function "std::pair<_T1, _T2

Get the type of the return value in C++

大城市里の小女人 提交于 2020-01-11 01:01:53
问题 Suppose we have a function f which returns a value of some unknown type (let's call it T ) and takes a value of the type T as an argument (and possibly has some other arguments). How do I get the return type of f in C++14? There is a way to do it if we know the know the argument types (via std::result_of ). Is it possible if we know all the argument types except T ? Example: template <class F> // F is functor with T operator()(T a, T b) class A { // Here I want to do // T some_function(T some