enable-if

Use std::tuple for template parameter list instead of list of types

守給你的承諾、 提交于 2019-12-01 06:50:54
I'm trying to make a call to a templated function like this : typedef std::tuple<int, double, bool> InstrumentTuple; Cache cache; InstrumentTuple tuple = cache.get<InstrumentTuple>(); I know I could "simply" pass the types of the tuple. This is what I do know but it is quite cumbersome since I make a lot of calls to this function and since the tuples are quite long: InstrumentTuple tuple = c.get<int, double, bool>(); // syntax I'd like to avoid So I tried multiple implementations of the get method, but with no success : Enabling via a template parameter #include <tuple> class Cache { private:

Using std::enable_if with anonymous type parameters

两盒软妹~` 提交于 2019-12-01 04:57:36
I try to use std::enable_if with an unused and unnamed type parameter, in order to not distort the return type. However, the following code does not compile. #include <iostream> template <typename T, typename = std::enable_if_t<!std::is_integral<T>::value>> T foo() { std::cout << "non-integral" << std::endl; return T(); } template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>> T foo() { std::cout << "integral" << std::endl; return T(); } int main() { foo<float>(); foo<int>(); } The compiler says: 7:3: error: redefinition of 'template<class T, class> T foo()' 4:3: note:

Convert template function to generic lambda

霸气de小男生 提交于 2019-11-30 14:11:30
问题 I'd like to pass templated functions around as if they were generic lambdas, however this does not work. #include <iostream> #include <vector> #include <tuple> #include <string> #include <utility> // for_each with std::tuple // (from https://stackoverflow.com/a/6894436/1583122) template<std::size_t I = 0, typename FuncT, typename... Tp> inline typename std::enable_if<I == sizeof...(Tp), void>::type for_each(std::tuple<Tp...> &, FuncT) {} template<std::size_t I = 0, typename FuncT, typename...

`std::enable_if` is function pointer - how?

泪湿孤枕 提交于 2019-11-30 12:08:37
I want to use SFINAE to enable a particular template if the user passes a function pointer as a parameter. I have googled around but found nothing - I also tried looking at the <type_traits> documentation but couldn't find anything that resembled a is_function_ptr<T> . By function pointer, I mean global function pointers, like TReturn(*)(TArgs...) . Below is a type trait determining if something is a function pointer and a couple of test cases. Note, that to test if something is a function pointer, you need to test if std::is_pointer<P>::value is true and if std::is_function<T>::value is true

Convert template function to generic lambda

点点圈 提交于 2019-11-30 09:49:30
I'd like to pass templated functions around as if they were generic lambdas, however this does not work. #include <iostream> #include <vector> #include <tuple> #include <string> #include <utility> // for_each with std::tuple // (from https://stackoverflow.com/a/6894436/1583122) template<std::size_t I = 0, typename FuncT, typename... Tp> inline typename std::enable_if<I == sizeof...(Tp), void>::type for_each(std::tuple<Tp...> &, FuncT) {} template<std::size_t I = 0, typename FuncT, typename... Tp> inline typename std::enable_if<I < sizeof...(Tp), void>::type for_each(std::tuple<Tp...>& t, FuncT

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

C++ templates: conditionally enabled member function

一世执手 提交于 2019-11-30 07:13:32
问题 I'm creating a very small C++ project, and I'd like to create a simple vector class for my own needs. The std::vector template class will not do. When the vector class is comprised of char s (i.e. vector<char> ), I'd like it to be able to be compared to a std::string . After a bit of messing around, I wrote code that both compiles and does what I want. See below: #include <string> #include <stdlib.h> #include <string.h> template <typename ElementType> class WorkingSimpleVector { public: const

What's the correct `enable_if` constraint on perfect forwarding setter?

Deadly 提交于 2019-11-30 04:51:56
Herb Sutter's Back to the Basics! Essentials of Modern C++ presentation at CppCon discussed different options for passing parameters and compared their performance vs. ease of writing/teaching. The 'advanced' option (providing the best performance in all the cases tested, but too difficult for most developers to write) was perfect forwarding, with the example given (PDF, pg. 28) : class employee { std::string name_; public: template <class String, class = std::enable_if_t<!std::is_same<std::decay_t<String>, std::string>::value>> void set_name(String &&name) noexcept( std::is_nothrow_assignable

Add/Remove data members with template parameters?

让人想犯罪 __ 提交于 2019-11-30 02:59:23
Consider the following code : template<bool AddMembers> class MyClass { public: void myFunction(); template<class = typename std::enable_if<AddMembers>::type> void addedFunction(); protected: double myVariable; /* SOMETHING */ addedVariable; }; In this code, the template parameter AddMembers allow to add a function to the class when it's true . To do that, we use an std::enable_if . My question is : is the same possible (maybe with a trick) for data members variable ? (in a such way that MyClass<false> will have 1 data member ( myVariable ) and MyClass<true> will have 2 data members (

`std::enable_if` is function pointer - how?

自作多情 提交于 2019-11-29 18:40:22
问题 I want to use SFINAE to enable a particular template if the user passes a function pointer as a parameter. I have googled around but found nothing - I also tried looking at the <type_traits> documentation but couldn't find anything that resembled a is_function_ptr<T> . By function pointer, I mean global function pointers, like TReturn(*)(TArgs...) . 回答1: Below is a type trait determining if something is a function pointer and a couple of test cases. Note, that to test if something is a