template-specialization

Function specialized template problem

江枫思渺然 提交于 2019-12-10 16:46:53
问题 I am new to templates. I try to define specialized template for function template, but my compiler returns error. It is simple max function, written just to practice templates; here's the code: template <typename TYP1, typename TYP2> TYP1 maximum(TYP1& param1, TYP2& param2) { return (param1 > param2 ? param1 : param2); } and specialized function: template<> std::string maximum<std::string, std::string>(std::string prm1, std::string prm2) { std::cout << "Inside specialized functiion\n"; return

Why is in-class partial specialization well-formed?

≯℡__Kan透↙ 提交于 2019-12-10 15:37:19
问题 According to [temp.class.spec] 5/ (emphasis mine) A class template partial specialization may be declared or redeclared in any namespace scope in which the corresponding primary template may be defined This would suggest that partial specialization (just like in case of explicit specialization) have to appear in the namespace scope. This is actually confirmed by the example below the paragraph: template<class T> struct A { struct C { template<class T2> struct B { }; }; }; // partial

Why Must Specializing Argument be void?

Deadly 提交于 2019-12-10 15:26:58
问题 So yet another question in this saga. Guillaume Racicot has been good enough to provide me with yet another workaround so this is the code I'll be basing this question off of: struct vec { double x; double y; double z; }; namespace details { template <typename T> using subscript_function = double(*)(const T&); template <typename T> constexpr double X(const T& param) { return param.x; } template <typename T> constexpr double Y(const T& param) { return param.y; } template <typename T> constexpr

Error: class template partial specialization contains a template parameter that cannot be deduced

爱⌒轻易说出口 提交于 2019-12-10 15:08:33
问题 I'd appreciate help figuring out what going on in this problem that's come up in my code which I've reduced to the following: typedef unsigned short ushort; template<typename T = ushort*> struct Foo { }; // Specialization -- works when not a specialization template< template<typename,typename> class Container , template<typename , template<typename,typename> class> class MetaFunction > struct Foo<Container<ushort,typename MetaFunction<ushort,Container>::Type> > { //typedef Container<ushort

Clang fails to compile template function in a template class specialization, which has *distinct return type* from the template declaration

为君一笑 提交于 2019-12-10 13:22:25
问题 The following function derefItemX() is compiled fine on GCC 4.8-5.3, but fails on CLang 3.8: //! Accessory Operations - template argument depended wrappers template<bool SIMPLE> // For Nodes / non-scoped storage struct Operations { //! \brief Defererence wrapped or direct iterator //! //! \param iel IItemXT& - iterator to be dereferenced //! \return ItemT& - resulting reference template<typename IItemXT> constexpr static auto& derefItemX(IItemXT& iel) { static_assert(is_base_of<std::forward

C++ template partial specialization: Why cant I match the last type in variadic-template?

£可爱£侵袭症+ 提交于 2019-12-10 12:45:01
问题 I try to write a IsLast type traits to check if a given type is the last one in a std::tuple , but the code below does not compile. I know how to get around it but I am curious why the compiler does not like it.I guess there must be some rule on specialization of variadic-template that I am not aware of. The code is at: https://godbolt.org/g/nXdodx Error message: error: implicit instantiation of undefined template 'IsLast<std::tuple<std::__cxx11::basic_string<char>, int>, int>' There is also

g++ and clang++ different behaviour when `std::make_index_sequence` and `std::index_sequence` are used for a template parameter default type

六眼飞鱼酱① 提交于 2019-12-10 12:44:33
问题 Another "who's right between g++ and clang++?" question for C++ standard gurus. Given the following code #include <utility> template <std::size_t N, typename = std::make_index_sequence<N>> struct foo; template <std::size_t N, std::size_t ... Is> struct foo<N, std::index_sequence<Is...>> { }; template <std::size_t N> void bar (foo<N> const &) { } int main() { bar(foo<42u>{}); } I see that g++ compile where clang++ gives the following error tmp_003-14,gcc,clang.cpp:32:4: error: no matching

Dependant non-type template parameter and variadic template

此生再无相见时 提交于 2019-12-10 10:15:18
问题 I am trying to extend the possibilities offered by std::integer_sequence with a new class named integer_range (which obiously creates a sequence of integers between two bounds). My implementation was based of my answer to this question that I tried to adapt to std::integer_sequence : namespace details { template<typename Int, Int C, Int P, Int... N> struct increasing_integer_range: increasing_integer_range<Int, C-1, P+1, N..., P> {}; template<typename Int, Int C, Int P, Int... N> struct

How partial template specialization chosen?

做~自己de王妃 提交于 2019-12-10 08:15:41
问题 Please explain me the rules for template specialization selection. I have an example: template<typename T1, typename T2 = int> struct S : false_type{}; template<typename T> struct S<T, float> : true_type{}; cout << boolalpha << S<float>::value; Why the output is false ? And in general, what happens with default template parameter typename T2 = int in specialized classes? Does it introduces some influence? 回答1: Choosing a template specialization happens in five steps: Take the primary template

C++ Templates: Partial Template Specifications and Friend Classes

不想你离开。 提交于 2019-12-10 03:00:42
问题 is it possible to somehow make a partial template specification a friend class? I.e. consider you have the following template class template <class T> class X{ T t; }; Now you have partial specializations, for example, for pointers template <class T> class X<T*>{ T* t; }; What I want to accomplish is that every possible X<T*> is a friend class of X<S> for ANY S . I.e. X<A*> should be a friend of X<B> . Of course, I thought about a usual template friend declaration in X: template <class T>