FAQ Database Discussion Community


Disable Function when parameter type is void

templates,c++11,c++14,sfinae,enable-if
I have a template class looking like this: template <typename T> constexpr bool is_value_passable_v = is_trivially_copyable_v<T> && sizeof(T) <= sizeof(void*) && !is_polymorphic_v<T>; template <typename B, typename T> using param_base_t = conditional_t<is_value_passable_v<B>, T, const T&>; template <typename T> struct param_d { using type = param_base_t<T, T>; }; template <> struct param_d<void>...

template method matching derived type instead of base

c++,templates,boost,enable-if,expression-templates
I have a set of operators that I need to override for expression templating. I would like all derived classes of a base type match to the base type. Other things would then be caught by a generic type. Unfortunately, the generic type grabs the derived types before the base...

enable_if in template Parameters Creates Template Redefinition Error

c++,templates,typename,redefinition,enable-if
In this answer what I really wanted to do is define a typename in my template parameters which could be used in the cast and return. So this: template <typename T> typename std::enable_if<sizeof(unsigned char) == sizeof(T), unsigned char>::type caster(T value){ return reinterpret_cast<unsigned char&>(value); } Would become this: template <typename T,...

JCheckBoxes not being disabled after I setEnable(false) them

java,swing,checkbox,jframe,enable-if
If have 30 checkboxes and I don't want more than 6 of them to be checked. I have an int keeping track of how many are checked. Each box has an item listener which calls a method to see what that int is at, and if it reaches 6, the...

Critical order of enable_if template functions

c++,c++11,metaprogramming,enable-if,template-function
Why is it important to the compiler to have the following order of definition for a visitor of a std::tuple namespace TupleVisit{ //This function SHOULD BE DEFINED SECONDLY; //IN THIS CONFIGURATION COMPILER DOES NOT //SEE THE BELOW RECURSTION TERMINATION ??? template<std::size_t Idx = 0, typename Visitor, typename... T, typename std::enable_if<...

C++: Enable method based on boolean template parameter

c++,templates,enable-if
I want to implement a private function based on a boolean template parameter. Something like that: #include <iostream> using namespace std; template <bool is_enabled = true> class Aggregator { public: void fun(int a) { funInternal(a); } private: void funInternal(int a, typename std::enable_if<is_enabled>::type* = 0) { std::cout << "Feature is enabled!"...

Conditional template specialization on method of a non template class

c++,templates,boost,template-specialization,enable-if
I am trying to use boost::enable_if to conditional specialize a method of a non-templated class, but failing at it. //OSSpecific.h ... //If some OS typedef unsigned int UINT; //else typedef unsigned long long UINT; ... //Foo.h ... #include <OSSpecific.h> ... class Foo { public: ... template <typename T> returnThis<T>* bar();...

Implementing a singleton wrapper class that also verifies 'the singleton constructor is private'

c++,templates,singleton,crtp,enable-if
I made a singleton wrapper template class that provides the instance() member function and is also supposed to assert whether the singleton class has a private constructor. The definition is as follows: template < class T, class = typename std::enable_if<!std::is_constructible<T>::value, void>::type > class singleton { public: static T& instance() {...

Understanding Alias Templates

c++,templates,alias,enable-if,c++1z
I asked a question that has several references to the code: template <typename...> using void_t = void; I believe I have a generally misunderstand alias templates: Why wouldn't you just evaluate whatever template parameter you're passing into an alias template in an enable_if_t or conditional_t statement? Is the code above...

std::enable_if sfine custom type via template parameter?

list,templates,c++11,stl,enable-if
First question, Let say that I have custom struct like this one: struct Var:{}; struct is_var_type:std::integral_constant<bool,std::is_same<Var, typename std::remove_cv<T>::type>::value > {}; After this, I want to have some custom type via template parameter sfine using std::enable_if, however I couldnt figure out. From what I could do was only following ways template<typename...

C++ ambigous overload for generic template ostream << operator

c++,templates,c++11,ostream,enable-if
This question follows my previous question : Generic operator<< ostream C++ for stringifiable class where I would like to implement a generic <<ostream operator which would work for any class which owns a to_str() method. I have succeeded checking whether a class implements a to_str() method and use std::cout <<...

How to force template substitution failure unless T::answer is in integral type?

c++,sfinae,enable-if
I have the following code: #include <type_traits> struct SA {}; struct SB { static const int answer = 42; }; const int SB::answer; template <typename T> int F() { return T::answer; } int main(int argc, char **argv) { (void)argc; (void)argv; // F<SA>(); // I want to make this a template...

std::enable_if using its internal type and without using it

c++,c++11,enable-if
Why does it print "B" #include <iostream> template<typename T, typename U = void> struct Test { static void apply() { std::cout << "A" << std::endl; } }; template<typename T> struct Test<T, typename std::enable_if<true>::type> { static void apply() { std::cout << "B" << std::endl; } }; int main() { Test<int>::apply(); }...

C++ template instantations: using enable_if directly, or with an auxiliary class

c++,templates,c++11,enable-if
This code works fine: #include <type_traits> using namespace std; enum class Type : char { Void }; struct FieldA { static constexpr Type type = Type::Void; }; template<typename Field> struct Signal {}; template<typename Field> struct SignalA : public Signal<Field> {}; struct SignalB : public Signal<void> {}; struct DerivedB : public...

Why specialization based on enable_if is not picked up by compiler

c++,c++11,template-specialization,enable-if
I'd like to specialize class for some type of classes, for example based on std::is_arithmetic. Although compiler doesn't "see" my specialization based on "enable_if" and chooses principle/main template. Could you please help me with this... Below is snippet of code and output after compiling with g++ 4.8 #include < iostream...

change the state of a textbox depending on which view is called

textbox,asp.net-mvc-5,conditional,enable-if
I'm new to MVC, and would like to know in C# mvc5, what kind of code I would need to use, if I wanted to change the state of a textbox from enabled to disabled and vice versa based on which view is calling the partial view.