c++14

How do I make template type deduction work with references?

自古美人都是妖i 提交于 2020-07-03 02:37:54
问题 I have a template function, func : template<typename T> void func(T p) { f(p); } And a set of functions f : f(SomeType&); f(int); ... If I instantiate the template function, func , using a reference as function argument p , without explicitly specifying template parameter T , then the type deduced will not be the reference type of p , but rather the type p is a reference to, for example: SomeType s; SomeType& ref_to_s = s; func(ref_to_s); // Type deduction results in: func<SomeType>(ref_to_s)

“Inverse SFINAE” to avoid ambiguous overload

一笑奈何 提交于 2020-06-29 07:03:54
问题 How can I prevent the first template below from instantiating if the second template instantiates? (i.e. if both static_cast<T>(0) and T::zero() are defined) template<typename T> auto zero() ->decltype(static_cast<T>(0)) { return static_cast<T>(0); } template<typename T> auto zero() ->decltype(T::zero()) { return T::zero(); } 回答1: If you need to extend it to multiple overloads with fine grained control of overload rank, a common technique is to use tag dispatching. template<int r> struct rank

“Inverse SFINAE” to avoid ambiguous overload

萝らか妹 提交于 2020-06-29 07:03:34
问题 How can I prevent the first template below from instantiating if the second template instantiates? (i.e. if both static_cast<T>(0) and T::zero() are defined) template<typename T> auto zero() ->decltype(static_cast<T>(0)) { return static_cast<T>(0); } template<typename T> auto zero() ->decltype(T::zero()) { return T::zero(); } 回答1: If you need to extend it to multiple overloads with fine grained control of overload rank, a common technique is to use tag dispatching. template<int r> struct rank

forcing template type to be reference by deduction

岁酱吖の 提交于 2020-06-29 06:54:10
问题 Say I have a function func : template<typename T> auto func(T arg){ std::cout << std::boolalpha; std::cout << "T is ref: " << std::is_reference<T>::value << '\n'; } Is there a way I can force T to be deduced as a reference type without explicitly specifying the template parameters? Like being able to write something like: auto main() -> int{ auto x = 5; func(std::ref(x)); } but without having to specialize for std::reference_wrapper . static_cast ing does not stop the decay of int& into int

Is it true that “std::forward” and “std::move” do not generate code?

有些话、适合烂在心里 提交于 2020-06-29 06:43:21
问题 Is it true that "std::forward" and "std::move" do not generate code? I saw this saying in << An Effective C++11/14 Sampler >>. The related code is at the footnote. Could somebody explain the code in detail? I would be very grateful to have some help with this question. As per the documentation(https://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00416_source.html), which says that: /** * @brief Forward an lvalue. * @return The parameter cast to the specified type. * * This function is

Assign a value to an rvalue reference returned from function

牧云@^-^@ 提交于 2020-06-27 07:17:32
问题 #include <utility> template <typename Container> decltype(auto) index(Container &&arr, int n) { return std::forward<Container>(arr)[n]; } Make a function call : #include <vector> index(std::vector {1, 2, 3, 4, 5}, 2) = 0; When function calling finished, the object std::vector {1, 2, 3, 4, 5} will be destroyed, assigning a value to a deallocated address would cause undefined behaviour. But the above code works well and valgrind detected nothing. Maybe the compile helps me make another

Not able to archive all the data

筅森魡賤 提交于 2020-06-23 12:37:05
问题 I am using boost for serialization of data. This is how the classes are structured. 1) I have a Stage class This class holds vector data for the director class class Stage { public: std::vector<Director> directors; void AddDirector(Director dir) { directors.push_back(dir); } int GetDirectorSize() { return directors.size(); } Director* GetDirector(int number) { return &directors[number]; } private: friend class boost::serialization::access; template<typename Archive> void save(Archive& ar,

Not able to archive all the data

荒凉一梦 提交于 2020-06-17 09:58:13
问题 I am using boost for serialization of data. This is how the classes are structured. 1) I have a Stage class This class holds vector data for the director class class Stage { public: std::vector<Director> directors; void AddDirector(Director dir) { directors.push_back(dir); } int GetDirectorSize() { return directors.size(); } Director* GetDirector(int number) { return &directors[number]; } private: friend class boost::serialization::access; template<typename Archive> void save(Archive& ar,

Using `reinterpret_cast` on an enum class - valid or undefined behavior?

北战南征 提交于 2020-06-17 06:47:51
问题 #include <iostream> #include <cassert> #include <type_traits> template<typename T> using Underlying = std::underlying_type_t<T>; enum class ETest : int { Zero = 0, One = 1, Two = 2 }; template<typename T> auto& castEnum(T& mX) noexcept { // `static_cast` does not compile // return static_cast<Underlying<T>&>(mX); return reinterpret_cast<Underlying<T>&>(mX); } int main() { auto x(ETest::Zero); castEnum(x) = 1; assert(x == ETest::One); return 0; } ideone Is this code guaranteed to always work?

In C++, does the size of an enumeration have to be equal to the size of its underlying type?

橙三吉。 提交于 2020-06-14 04:07:54
问题 I often assume that size of an enumeration is the same as the size of its underlying type. But is it mandated by the standard? The standard (C++14, n4296) says that every enumeration has an underlying type (7.2/5). The standard also says that objects are represented as sequences of bytes, and that the size of an object is related to its representation: 3.9/4 The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T, where N