template-specialization

Where should I define operator >> for my specialization of std::pair?

拜拜、爱过 提交于 2019-11-29 03:27:52
Consider the following program: #include <iostream> #include <iterator> #include <vector> #include <utility> using namespace std; //just for convenience, illustration only typedef pair<int, int> point; //this is my specialization of pair. I call it point istream& operator >> (istream & in, point & p) { return in >> p.first >> p.second; } int main() { vector<point> v((istream_iterator<point>(cin)), istream_iterator<point>()); // ^^^ ^^^ //extra parentheses lest this should be mistaken for a function declaration } This fails to compile because as soon as ADL finds operator >> in namespace std it

How to specialize only some members of a template class?

眉间皱痕 提交于 2019-11-29 02:56:27
问题 Code: template<class T> struct A { void f1() {}; void f2() {}; }; template<> struct A<int> { void f2() {}; }; int main() { A<int> data; data.f1(); data.f2(); }; ERROR: test.cpp: In function 'int main()': test.cpp:16: error: 'struct A<int>' has no member named 'f1' Basically, I only want to specialize one function and use the common definition for other functions. (In actual code, I have many functions which I don't want to specialize). How to do this? Thanks! 回答1: Consider moving common parts

enable_if method specialization

僤鯓⒐⒋嵵緔 提交于 2019-11-29 01:29:54
问题 template<typename T> struct A { A<T> operator%( const T& x); }; template<typename T> A<T> A<T>::operator%( const T& x ) { ... } How can I use enable_if to make the following specialization happen for any floating point type (is_floating_point)? template<> A<float> A<float>::operator%( const float& x ) { ... } EDIT: Here's an answer I came up which is different from the ones posted below... template<typename T> struct A { T x; A( const T& _x ) : x(_x) {} template<typename Q> typename std:

Can I specialize a class template with an alias template?

送分小仙女□ 提交于 2019-11-28 23:34:29
Here's a simple example: class bar {}; template <typename> class foo {}; template <> using foo<int> = bar; Is this allowed? $ clang++ -std=c++0x test.cpp test.cpp:6:1: error: explicit specialization of alias templates is not permitted template <> ^~~~~~~~~~~ 1 error generated. Reference: 14.1 [temp.decls]/p3: 3 Because an alias-declaration cannot declare a template-id, it is not possible to partially or explicitly specialize an alias template. Aotium Although direct specialization of the alias is impossible, here is a workaround. (I know this is an old post but it's a useful one.) You can

Conversion operator template specialization

家住魔仙堡 提交于 2019-11-28 23:17:59
问题 Here's a largely academic exercise in understanding conversion operators, templates and template specializations. The conversion operator template in the following code works for int , float , and double , but fails when used with std::string ... sort of. I've created a specialization of the conversion to std::string , which works when used with initialization std::string s = a; , but fails when used with a cast static_cast<std::string>(a) . #include <iostream> #include <string> #include

Function template specialization format

落花浮王杯 提交于 2019-11-28 17:14:32
What is the reason for the second brackets <> in the following function template: template<> void doh::operator()<>(int i) This came up in SO question where it was suggested that there are brackets missing after operator() , however I could not find the explanation. I understand the meaning if it was a type specialization (full specialization) of the form: template< typename A > struct AA {}; template<> struct AA<int> {}; // hope this is correct, specialize for int However for function templates: template< typename A > void f( A ); template< typename A > void f( A* ); // overload of the above

Specializing std::hash to derived classes

旧城冷巷雨未停 提交于 2019-11-28 13:53:06
I have an abstract base class Hashable that classes that can be hashed derive from. I would now like to extend std::hash to all classes that derive from Hashable . The following code is supposed to do exactly that. #include <functional> #include <type_traits> #include <iostream> class Hashable { public: virtual ~Hashable() {} virtual std::size_t Hash() const =0; }; class Derived : public Hashable { public: std::size_t Hash() const { return 0; } }; // Specialization of std::hash to operate on Hashable or any class derived from // Hashable. namespace std { template<class C> struct hash {

specializing iterator_traits

时光毁灭记忆、已成空白 提交于 2019-11-28 11:16:33
I'd like to specialize std::iterator_traits<> for iterators of a container class template that does not have the usual nested typedefs (like value_type , difference_type , etc.) and whose source I shouldn't modify. Basically I'd like to do something like this: template <typename T> struct iterator_traits<typename Container<T>::iterator> { typedef T value_type; // etc. }; except that this doesn't work, as the compiler is unable to deduce T from Container<T>::iterator . Is there any working way to achieve the same? For example: template <typename T> class SomeContainerFromAThirdPartyLib {

std::hash specialization using sfinae?

此生再无相见时 提交于 2019-11-28 11:16:23
As an exercise I was trying to see if I could use SFINAE to create a std::hash specialization for std::pair and std::tuple when all of its template parameters are of an unsigned type. I have a little experience with them, but from what I understand the hash function needs to have already been templated with a typename Enabled = void for me to add a specialization. I'm not really sure where to go from here. Here's an attempt which doesn't work. #include <functional> #include <type_traits> #include <unordered_set> #include <utility> namespace std { template <typename T, typename Enabled = void>

Partial specialization of a method in a templated class

拈花ヽ惹草 提交于 2019-11-28 10:45:23
Given: struct A { virtual bool what() = 0; }; template<typename T, typename Q> struct B : public A { virtual bool what(); }; I want to partially specialize what like: template<typename T, typename Q> bool B<T, Q>::what() { return true; } template<typename Q> bool B<float, Q>::what() { return false; } But it appears that this isn't possible (is it in C++11?) so I tried SFINAE: template<typename T> typename std::enable_if<std::is_same<T, float>::value, bool>::type B<T>::what() { return true; } template<typename T> typename std::enable_if<!std::is_same<T, float>::value, bool>::type B<T>::what() {