c++14

C++: Expand array elements as parameter of a function using boost::hana

混江龙づ霸主 提交于 2019-12-07 16:19:41
问题 I discovered two months ago boost::hana. Seems very powerfull so I decided to take a look. From the documentation I saw this examples: std::string s; hana::int_c<10>.times([&]{ s += "x"; }); that is equivalent to: s += "x"; s += "x"; ... s += "x"; // 10 times I'd like to know if it is possible (and if yes how) to write smthg like: std::string s; std::array<int, 10> xs = {1, 3, 5, ...}; hana::int_c<10>.times([&](int i){ s += std::to_string(xs[i]) + ","; }); a sort of "unpacking" at compile

Custom range for boost::range library

血红的双手。 提交于 2019-12-07 15:59:00
问题 I’m writing filter and map algorithms using boost::range library: template <class Range> struct Converter { Converter(const Range& p_range) : m_range(p_range) {} template<class OutContainer> operator OutContainer() const { return {m_range.begin(), m_range.end()}; } private: Range m_range; }; template<class Range> Converter<Range> convert(const Range& p_range) { return {p_range}; } template<class Range, class Fun> auto map(Range&& p_range, Fun&& p_fun) { return convert(p_range | boost:

Why does order of declaring function changes overload chosen by SFINAE?

╄→尐↘猪︶ㄣ 提交于 2019-12-07 15:48:30
问题 This question is related to this answer. In this example SFINAE uses variable template has_literal_x specialization instead of the base template: struct A { }; A operator"" _x(char const*) { return {}; } template<typename T, typename S, typename = void> constexpr bool has_literal_x = false; template<typename T, typename S> constexpr bool has_literal_x<T, S, std::enable_if_t< std::is_same< decltype(operator""_x(std::declval<S>())), T >::value > > = true; int main() { std::cout << has_literal_x

How to represent a variadic templated type based on another group of variadic template arguments in modern C++?

狂风中的少年 提交于 2019-12-07 14:34:08
问题 Suppose I have a following variadic template structure: template <class... T> struct Example {}; Now I want to define a template function: template<class... S> ??? f() { return Example<???> } where the specialization of Example<> is depend on the template parameter S of f . To be more concrete (and simple), now I just want to return Example<int, ...,int> , where the number of int is the size of the parameter pack S . How can it be done in modern C++, i.e. C++11/14/17? More generally, is there

Perfect forwarding to a member function of a data member?

可紊 提交于 2019-12-07 14:19:35
问题 Consider a class that has a private std::vector data member: class MyClass { private: std::vector<double> _data; public: template <class... Args> /* something */ insert(Args&&... args) /* something */ { return _data.insert(std::forward<Args>(args)...); } }; What is the correct syntax (using C++14 auto/variadic templates/forward...) to transfer a given function of _data to MyClass (for example insert here) and provide the same interface for the user? 回答1: The correct syntax is this: class

How could the exception specifier on move assignment operator affect that of move constructor?

坚强是说给别人听的谎言 提交于 2019-12-07 13:32:22
问题 I've being testing with GCC 5.2 and clang 3.6, both in C++14 mode, and they give the same output. For the following code #include <iostream> #include <type_traits> struct S { // S& operator= (S&&) noexcept { return *this; } }; int main() { std::cout << std::is_nothrow_move_constructible<S>::value << std::is_nothrow_move_assignable<S>::value; } the result 11 is obtained. But if uncomment the move assignment operator, the output becomes 01 . How could an explicit noexcept specification on the

Build template from arguments of functions?

对着背影说爱祢 提交于 2019-12-07 12:29:24
问题 template<class... Foos> // N = sizeof...(Foos) template<typename... Args> // M = sizeof...(Args) void split_and_call(Args&&... args) { // Using Python notation here... Foos[0](*args[:a]); // a = arity of Foos[0] Foos[1](*args[a:b]); // b-a = arity of Foos[1] ... Foos[N-1](*args[z:M]); // M-z = arity of Foos[N-1] } Assumptions: All types in Foos are callable All types in Foos are unambiguous Any type in Foos may have an arity of 0 Args is the concatenation of all of the argument types used by

Can static/dynamic/const/reinterpret_cast be used in unevaluated context?

巧了我就是萌 提交于 2019-12-07 11:47:45
问题 I tried to provide structures for checking is A is (choose cast)-castable to B . All four casts would have exact same implementation, expect their names (local-macro-able definitions would be possible, but not necessary). I wrote many check-for operators structures, for example: #include <iostream> #include <type_traits> #include <string> template<class, class, class, class = void> struct is_valid_ternary_operator : std::false_type { }; template<class T, class S, class R> struct is_valid

reserve() function for Vector Of Strings in C++

泄露秘密 提交于 2019-12-07 11:45:35
问题 I am trying to populate a vector of string type and the memory for the strings will be updated periodically.I found out in a forum that, both of these processes consume a lot of time due to memory reallocation every time I update the size and I also read that the reserve function solves the problem pretty much for both the cases. -> String & vector My vector wont need more than 1024 slots and each string will need 10 character spaces. I have reserved 1024 memory slots for my vector. vector

Using of not captured variable in lambda

牧云@^-^@ 提交于 2019-12-07 11:33:37
问题 I can not quite understand an example from C++14 standard draft N4140 5.1.2.12 [expr.prim.lambda] . A lambda-expression with an associated capture-default that does not explicitly capture this or a variable with automatic storage duration (this excludes any id-expression that has been found to refer to an initcapture’s associated non-static data member), is said to implicitly capture the entity (i.e., this or a variable) if the compound-statement: odr-uses the entity, or names the entity in a