sfinae

enable_if iterator as a default template parameter?

孤人 提交于 2019-11-30 07:39:35
问题 I have a constructor like that : class MyClass { template<class TI> MyClass(TI first, TI last); }; template<class TI> MyClass::MyClass(TI first, TI last) { ; } I would like to enable this constructor only if TI is an iterator (that means TI has an iterator_category I think). How to do that in C++ 2011 using an enable_if as a default template parameter (in the declaration and in the definition) ? Thank you very much. 回答1: It depends on what you want. If there are no other overloads, it can be

(Im)perfect forwarding with variadic templates

时光毁灭记忆、已成空白 提交于 2019-11-30 03:39:14
Synopsis Given a type with a variadic template constructor that forwards the arguments to an implementation class, is it possible to restrict the types being forwarded with SFINAE? Details First, consider the non-variadic case with a constructor taking a universal reference. Here one can disable forwarding of a non-const lvalue reference via SFINAE to use the copy constructor instead. struct foo { foo() = default; foo(foo const&) { std::cout << "copy" << std::endl; } template < typename T, typename Dummy = typename std::enable_if< !std::is_same< T, typename std::add_lvalue_reference<foo>::type

Is it possible to check if a user literal is defined for given type and argument?

倾然丶 夕夏残阳落幕 提交于 2019-11-30 01:51:24
问题 I want to check at compile-time if user literal _name is defined for type Ret and argument Arg . While I have half-solution, it requires the literal operator to be defined at least once: #include <iostream> #include <type_traits> struct one { }; struct two { }; // we need at least one of these definitions for template below to compile one operator"" _x(char const*) {return {};} two operator"" _x(unsigned long long int) {return {};} template<class T, class S, class = void> struct has_literal_x

How to avoid this sentence is false in a template SFINAE?

折月煮酒 提交于 2019-11-30 01:35:04
So I want to write an automatic != : template<typename U, typename T> bool operator!=(U&& u, T&& t) { return !( std::forward<U>(u) == std::forward<T>(t) ); } but that is impolite 1 . So I write // T() == U() is valid? template<typename T, typename U, typename=void> struct can_equal:std::false_type {}; template<typename T, typename U> struct can_equal< T, U, typename std::enable_if< std::is_convertible< decltype( std::declval<T>() == std::declval<U>() ), bool >::value >::type >: std::true_type {}; which is a type traits class that says "is t == u valid code that returns a type convertible to

What is decltype with two arguments?

早过忘川 提交于 2019-11-29 17:33:26
Edit, in order to avoid confusion: decltype does not accept two arguments. See answers. The following two structs can be used to check for the existance of a member function on a type T during compile-time: // Non-templated helper struct: struct _test_has_foo { template<class T> static auto test(T* p) -> decltype(p->foo(), std::true_type()); template<class> static auto test(...) -> std::false_type; }; // Templated actual struct: template<class T> struct has_foo : decltype(_test_has_foo::test<T>(0)) {}; I think the idea is to use SFINAE when checking for the existance of a member function, so

What trait / concept can guarantee memsetting an object is well defined?

心已入冬 提交于 2019-11-29 16:37:22
问题 Let's say I have defined a zero_initialize() function: template<class T> T zero_initialize() { T result; std::memset(&result, 0, sizeof(result)); return result; } // usage: auto data = zero_initialize<Data>(); Calling zero_initialize() for some types would lead to undefined behavior 1, 2 . I'm currently enforcing T to verify std::is_pod. With that trait being deprecated in C++20 and the coming of concepts, I'm curious how zero_initialize() should evolve. What (minimal) trait / concept can

C++11 style SFINAE and function visibility on template instantiation

≡放荡痞女 提交于 2019-11-29 16:00:11
I'm not sure if this has anything to do with sfinae, or just something thats relevant for any templated function. I am attempting to use sfinae to enable/disable a member function based on existence of corresponding free function, which in turn is enabled/disabled based on existance of member function in another type, all using method described here : struct S; template <typename T> inline auto f(S& s, T const& t) -> decltype(t.f(s), void()) { t.f(s); } struct S { template <typename T> auto f(T const& t) -> decltype(f(*this, t), void()) { f(*this, t); // <--------------------------------------

Check if two types are of the same template

倾然丶 夕夏残阳落幕 提交于 2019-11-29 15:43:39
I want to check if two types are of the same template. As an example I want the following snippet of code to return true because both objects are vectors despite the inner elements being of different types. It's important that the check is made at compile time (that's why the function is constexpr). #include <iostream> #include <type_traits> #include <vector> template <typename Container1, typename Container2> constexpr bool CheckTypes(Container1 c1, Container2 c2) { return std::is_same<Container1,Container2>::value; } int main() { std::vector<int> v1(100,0); std::vector<double> v2(100,0); std

checking if a class inherits from any template instantiation of a template

本秂侑毒 提交于 2019-11-29 14:58:05
问题 I have written a small utility for testing whether or not a type has inherited some template instantiation of a specific template class, either directly or trough inheriting a class that inherits the template. This is accomplished with a SFINAE check using a template function accepting any template instantiation of the provided template and a fallback overload for the default case. #include <iostream> #include <type_traits> template<template<class> class T, class U> struct isDerivedFrom {

Is it possible to write c++ template/macros to check whether two functions have the same signatures

老子叫甜甜 提交于 2019-11-29 14:34:27
Is it possible to write c++ template/macros to check whether two functions have the same signatures (return type and arguments list) ? Here's a simple example of how I want to use it: int foo(const std::string& s) {...} int bar(const std::string& s) {...} if (SAME_SIGNATURES(foo, bar)) { // do something useful... make Qt signal-slot connection for example... } else { // signatures mismatch.. report a problem or something... } So is it possible somehow or is it just a pipe dream ? P.S. Actually I'm interesting in c++ 2003 standard. C++11 Solution No need to write any template yourself. You can