template-specialization

Partial specialization of member function [duplicate]

大城市里の小女人 提交于 2019-12-19 19:44:35
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: “invalid use of incomplete type” error with partial template specialization Why is it that I can do this: template <typename T> struct A { void foo(int); }; template <> void A<int>::foo(int) { } but not this: template <typename> struct C {}; template <typename T> struct A { void foo(int); }; template <typename T> void A<C<T> >::foo(int) { } For the second case, GCC gives the following error: test.cpp:10:23:

Smplest way to provide template specialization for derived classes

二次信任 提交于 2019-12-19 09:09:15
问题 I have following scenario: class my_base { ... } class my_derived : public my_base { ... }; template<typename X> struct my_traits. I want to specialize my_traits all classes derived from my_base including: i.e. template<typname Y> // Y is derived form my_base. stryct my_traits { ... }; I have no problems to add any tags, members to my_base to make it simpler. I've seen some trick but I still feel lost. How can this be done is simple and short way? 回答1: Well, you don't need to write your own

What does SFINAE not work correctly with following has_member function?

强颜欢笑 提交于 2019-12-19 00:45:34
问题 I'm trying out examples from Walter Brown's TMP talk and I'm trying to get his has_member implementation working. However the implementation seems to falsely return true which leads me to believe there is some detail of SFINAE that I am not understanding. #include <iostream> #include <type_traits> template <class ...> using void_t = void; template <class, class = void> struct has_type_member: std::false_type {}; template <class T> struct has_type_member<T, void_t<typename T::type> >: std:

Is sizeof… allowed in template arguments for specialization?

泪湿孤枕 提交于 2019-12-18 17:04:57
问题 I'm trying to do something along the lines of this using GCC 4.7 snapshot: template <int n, int... xs> struct foo { static const int value = 0; }; // partial specialization where n is number of ints in xs: template <int... xs> struct foo<sizeof...(xs), xs...> { // error: template argument ‘sizeof (xs ...)’ // involves template parameter(s) static const int value = 1; }; template <int... xs> struct foo<sizeof(xs), xs...> { // This compiles fine. sizeof(xs) is sizeof int // even though packs

Is it possible to specialize a template using a member enum?

爷,独闯天下 提交于 2019-12-18 11:52:44
问题 struct Bar { enum { Special = 4 }; }; template<class T, int K> struct Foo {}; template<class T> struct Foo<T,T::Special> {}; Usage: Foo<Bar> aa; fails to compile using gcc 4.1.2 It complains about the usage of T::Special for partial specilization of Foo. If Special was a class the solution would be to a typename in front of it. Is there something equivalent to it for enums (or integers)? 回答1: Since that is not allowed by C++ as explained by Prasoon, so an alternative solution would be to use

Specializing C++ template based on presence/absense of a class member?

こ雲淡風輕ζ 提交于 2019-12-18 04:12:31
问题 Consider the following: struct A { typedef int foo; }; struct B {}; template<class T, bool has_foo = /* ??? */> struct C {}; I want to specialize C so that C<A> gets one specialization and C<B> gets the other, based on the presence or absence of typename T::foo. Is this possible using type traits or some other template magic? The problem is that everything I've tried produces a compile error when instantiating C<B> because B::foo doesn't exist. But that's what I want to test! Edit: I think

Specializing C++ template based on presence/absense of a class member?

隐身守侯 提交于 2019-12-18 04:12:26
问题 Consider the following: struct A { typedef int foo; }; struct B {}; template<class T, bool has_foo = /* ??? */> struct C {}; I want to specialize C so that C<A> gets one specialization and C<B> gets the other, based on the presence or absence of typename T::foo. Is this possible using type traits or some other template magic? The problem is that everything I've tried produces a compile error when instantiating C<B> because B::foo doesn't exist. But that's what I want to test! Edit: I think

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

Deadly 提交于 2019-12-18 04:00:45
问题 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 template specialization format

柔情痞子 提交于 2019-12-17 22:09:09
问题 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

Explicit specialization of function templates causes linker error

给你一囗甜甜゛ 提交于 2019-12-17 18:55:58
问题 Functions.h: #pragma once #include <iostream> template<class T> void TemplatedFunction(T* p) {} template<> void TemplatedFunction<float>(float* p) {} template<> void TemplatedFunction<char>(char* p) {} Functions.cpp: #include "Functions.h" void Test() { TemplatedFunction<float>(NULL); TemplatedFunction<char>(NULL); } main.cpp: #include "Functions.h" void Test(); int main() { Test(); return 0; } Build errors: main.obj : error LNK2005: "void __cdecl TemplatedFunction<float>(float *)" (??