c++14

Boost.Hana: How to check if function has specialisation for a certain type?

情到浓时终转凉″ 提交于 2019-12-04 18:10:31
I have a template function that has no definition by default but it specialised by some types: template <typename T> auto foo(bar &, const T &) -> void; template <> auto foo<std::string>(bar &, const std::string &) -> void {} How do I write a constexpr function that tells me if type T has a specialisation for the above function? My best effort: namespace detail { auto has_foo(hana::is_valid([](auto &b, const auto &t) -> decltype(foo(b, t)) {})); } // namespace detail template <typename T> constexpr auto has_foo() -> bool { using hana::type_c; return detail::has_foo(type_c<bar>, type_c<T>); }

Check if member function exists and is not inherited for SFINAE

。_饼干妹妹 提交于 2019-12-04 17:40:36
How can I check if a member function exists and is not inherited? I need this to resolve ambiguity for the following example: A type either has a foo() or a bar() member function. Caller will call the one that exists for the given type. However, DerivedWithBar inherits foo() from BaseWithFoo but defines its own bar() . Thus, Caller does not know which function to call. I'd need a way to give the non-inherited foo precedence over the inherited bar() , but I do not know how to check if a member function is inherited or not. #include <iostream> struct BaseWithFoo { template <typename T> void foo

Why is a template with deduced return type not overloadable with other versions of it?

爷,独闯天下 提交于 2019-12-04 17:39:32
问题 Why are the following two templates incompatible and can't be overloaded? #include <vector> template<typename T> auto f(T t) { return t.size(); } template<typename T> auto f(T t) { return t.foobar(); } int main() { f(std::vector<int>()); } I would think they are (more or less) equivalent with the following which compiles fine (as we cannot do decltype auto(t.size()) I can't give an exact equivalent without some noise..). template<typename T> auto f(T t) -> decltype(t.size() /* plus some decay

how to improve performance of boost::spirit::x3 key-value parser

允我心安 提交于 2019-12-04 17:10:55
I am parsing key value pairs (similar to HTTP headers) using boost::spirit::x3 . When comparing the performance to my handwritten parser, boost::spirit::x3 is around 10% slower than that. I am using boost 1.61 and GCC 6.1: $ g++ -std=c++14 -O3 -I/tmp/boost_1_61_0/boost/ main.cpp && ./a.out phrase_parse 1.97432 microseconds parseHeader 1.75742 microseconds How can I improve the performance of the boost::spirit::x3 based parser? #include <iostream> #include <string> #include <map> #include <chrono> #include <boost/spirit/home/x3.hpp> #include <boost/fusion/adapted/std_pair.hpp> using header_map

Why is there no std::is_transparent equivalent for unordered containers?

不羁岁月 提交于 2019-12-04 16:50:12
问题 C++14 introduces Compare::is_transparent for equivalent find operations in associative containers. template< class K > iterator find( const K& x ); template< class K > const_iterator find( const K& x ) const; Finds an element with key that compares equivalent to the value x. This overload only participates in overload resolution if the qualified-id Compare::is_transparent is valid and denotes a type. It allows calling this function without constructing an instance of Key Since there is no

Are implementations allowed to add public members to standard types?

天涯浪子 提交于 2019-12-04 16:40:24
问题 Are C++ standard library implementations allowed to add public (and protected) members to standard types' interfaces? N3797 17.6.5.5 [member.functions]/2 says: An implementation may declare additional non-virtual member function signatures within a class: — by adding arguments with default values to a member function signature; [ Note : An implementation may not add arguments with default values to virtual, global, or non-member functions. — end note ] — by replacing a member function

(v) is actually (*&v) since when?

ぃ、小莉子 提交于 2019-12-04 16:09:10
问题 Could C++ standards gurus please enlighten me: Since which C++ standard version has this statement failed because (v) seems to be equivalent to (*&v) ? I.e. for example the code: #define DEC(V) ( ((V)>0)? ((V)-=1) : 0 ) ...{... register int v=1; int r = DEC(v) ; ...}... This now produces warnings under -std=c++17 like: cannot take address of register variable left hand side of operand must be lvalue Many C macros enclose ALL macro parameters in parentheses, of which the above is meant only to

Can I write a function type that returns a function?

為{幸葍}努か 提交于 2019-12-04 16:07:04
问题 The following fails to compile on both gcc and clang #include <type_traits> int foo(); int main() { using R = std::result_of_t<decltype(foo)()>; // error } The error on both compilers deals with the illegality of declaring a function returning a function. But I'm not declaring such a function - I'm just trying to write its type - since that's what result_of expects. Is this really still ill-formed? 回答1: You're passing a type-id , which is defined in [dcl.name] as […] syntactically a

Return type deduction with a private member variable

北城余情 提交于 2019-12-04 16:02:11
问题 As was explained in this Q&A yesterday, both g++ 4.8 and Clang 3.3 correctly complain about the code below with an error like "'b_' was not declared in this scope" #include <iostream> class Test { public: Test(): b_(0) {} auto foo() const -> decltype(b_) // just leave out the -> decltype(b_) works with c++1y { return b_; } private: int b_; }; int main() { Test t; std::cout << t.foo(); } Moving the private section to the top of the class definition eliminates the error and prints 0. My

Stripping all qualifiers from a function type

左心房为你撑大大i 提交于 2019-12-04 16:01:28
问题 Given a possibly varargs function type with possibly a cv-qualifier-seq and possibly a ref-qualifier , is it possible to write a type trait that strips all the qualifiers without writing 4 * 3 * 2 = 24 partial specializations? template<class T> struct strip_function_qualifiers; template<class R, class... Args> struct strip_function_qualifiers<R(Args...)> { using type = R(Args...); }; template<class R, class... Args> struct strip_function_qualifiers<R(Args..., ...)> { using type = R(Args..., .