enable-if

Forward declaring a function that uses enable_if : ambiguous call

I have some trouble forward declaring a function that uses boost::enable_if: the following piece of code gives me a compiler error: // Declaration template <typename T> void foo(T t); // Definition template <typename T> typename boost::enable_if<boost::is_same<T, int> >::type foo(T t) { } int main() { foo(12); return 0; } Wh...

How can I write a function template for all types with a particular type trait?

Consider the following example: struct Scanner { template <typename T> T get(); }; template <> string Scanner::get() { return string("string"); } template <> int Scanner::get() { return 10; } int main() { Scanner scanner; string s = scanner.get<string>(); int i = scanner.get<int>(); } The Scanner class i...

enable_if and conversion operator?

Any chance to use enable_if with a type conversion operator? Seems tricky, since both return type and parameters list are implicit. ...

Where is disable_if in C++0x?

Boost has both enable_if and disable_if, but C++0x seems to be missing the latter. Why was it left out? Are there meta-programming facilities in C++0x that allow me to build disable_if in terms of enable_if? Oh, I just noticed that std::enable_if is basically boost::enable_if_c, and that there is no such thing as boost::enable_if in C...

Strange enable_if behaviour using nested classes (MSVC compiler bug or feature?)

After quite some time debugging my code, I tracked down the reason for my problems to some unexpected template specialization results using enable_if: The following code fails the assertion in DoTest() in Visual Studio 2010 (and 2008), while it doesn't in g++ 3.4.5. However, when i remove the template from SomeClass or move *my_conditio...

enable_if + disable_if combination provokes an ambiguous call

While trying to answer this question I wanted to suggest the use of enable_if + disable_if to allow the overload of a method based on the fact that a type was (or not) polymorphic. So I created a small test file: template <class T> void* address_of(T* p, boost::enable_if< boost::is_polymorphic<T> >* dummy = 0) { return...

C++ boost enable_if question

Hello! Do I have any way to simplify the following statements? (probably, using boost::enable_if). I have a simple class structure - Base base class, Derived1, Derived2 inherit from Base. I have the following code: template <typename Y> struct translator_between<Base, Y> { typedef some_translator<Base, Y> type; }; template <typen...

Why can't the type be deduced in this template function?

template<typename T> std::istream & read(std::istream & istr, typename std::enable_if<std::is_pod<T>::value, T>::type & value) { return istr.read( reinterpret_cast<char*>(&value), sizeof(T)); } int main() { int x; read(cin, x); // error here } error C2783: 'std::istream &read(std::istream &,std::enable_if<std::tr1::is_pod...