template-specialization

Is std::vector<T> a `user-defined type`?

我与影子孤独终老i 提交于 2019-11-27 19:16:41
In 17.6.4.2.1/1 and 17.6.4.2.1/2 of the current draft standard restrictions are placed on specializations injected by users into namespace std . The behavior of a C ++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited. I cannot find

“template<>” vs “template” without brackets - what's the difference?

和自甴很熟 提交于 2019-11-27 18:39:19
Suppose I've declared: template <typename T> void foo(T& t); Now, what is the difference between template <> void foo<int>(int& t); and template void foo<int>(int& t); semantically? And do template-with-no-brackets and template-with-empty-brackets have other semantics in other contexts? Related to: How do I force a particular instance of a C++ template to instantiate? Mark B template <> void foo<int>(int& t); declares a specialization of the template, with potentially different body. template void foo<int>(int& t); causes an explicit instantiation of the template, but doesn't introduce a

Template class member specialization without declaration in header

为君一笑 提交于 2019-11-27 17:46:49
问题 I have a template class that I declare in a header with one method and no definition of that method in the header. In a .cc file, I define specializations of that method without ever declaring them in the header . In a different .cc file, I call the method for different template parameters for which specializations exist. It looks like this: foo.h: template<typename T> class Foo { public: static int bar(); }; foo.cc: #include "foo.h" template<> int Foo<int>::bar() { return 1; } template<> int

Can I specialize a class template with an alias template?

坚强是说给别人听的谎言 提交于 2019-11-27 15:05:33
问题 Here's a simple example: class bar {}; template <typename> class foo {}; template <> using foo<int> = bar; Is this allowed? 回答1: $ 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. 回答2: Although direct specialization of the

Specialized template function with deleted “general” case fails to compile with g++ <=4.8.0 and clang++

醉酒当歌 提交于 2019-11-27 15:01:06
Compiling a project with an older version of g++ (4.8.0, MinGW) I found that this code fails to compile: template<typename T> void foo() = delete; template<> void foo<int>(){} int main() { foo<int>(); return 0; } It seems that g++ doesn't even try to look for explicit specializations if it sees that the base case is deleted. mitalia@mitalia:~/scratch$ /opt/mingw32-dw2/bin/i686-w64-mingw32-g++ -std=c++11 buggy_deleted_template.cpp buggy_deleted_template.cpp: In function 'int main()': buggy_deleted_template.cpp:8:14: error: use of deleted function 'void foo() [with T = int]' foo<int>(); ^ buggy

Why is it not possible to overload class templates?

懵懂的女人 提交于 2019-11-27 13:51:38
Reading this question made me wonder: is there a technical reason for disallowing class templates overloads? By overloading, I mean having several templates with the same names, but different parameters, for instance template <typename T> struct Foo {}; template <typename T1, typename T2> struct Foo {}; template <unsigned int N> struct Foo {}; The compiler manages to handle overloaded functions and function templates, wouldn't it be possible to apply the same techniques (e.g. name mangling) to class templates? At first, I thought that perhaps that would cause some ambiguity issues when taking

std::hash specialization using sfinae?

十年热恋 提交于 2019-11-27 06:10:19
问题 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>

specializing iterator_traits

别等时光非礼了梦想. 提交于 2019-11-27 06:10:02
问题 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

C++ specialization of template function inside template class

老子叫甜甜 提交于 2019-11-27 05:39:11
问题 What is the C++ syntax for specializing a template function that's inside a template class? For example, consider that I have the following two classes and their usage. I would like to be able to provide specialized implementations of the method X::getAThing() for different types. E.g.: int, std::string, arbitrary pointer or class, etc. template <class c1> class X { public: template<typename returnT> returnT getAThing(std::string param); static std::string getName(); private: c1 theData; }; /

How to decide if a template specialization exist

二次信任 提交于 2019-11-27 04:36:55
问题 I would like to check if a certain template specialization exist or not, where the general case is not defined. Given: template <typename T> struct A; // general definition not defined template <> struct A<int> {}; // specialization defined for int I would like to define a struct like this: template <typename T> struct IsDefined { static const bool value = ???; // true if A<T> exist, false if it does not }; Is there a way to do that (ideally without C++11)? Thanks 回答1: Using the fact that you