473,394 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Partial specialisation

Hi,

The following code is legal, and works as expected:

#include <iostream>

template <typename T>
class Bar {
};

template <typename T, typename P>
class Foo {
public:
void doStuff();
};
template <typename T, typename P>
void Foo<T, P>::doStuff() {
std::cout << "General" << std::endl;
}

int main() {
Foo<float, Bar<float a;
a.doStuff();

Foo<float, floatb;
b.doStuff();

return 0;
}
But none of these specialise the method doStuff():

// Not valid:
template <typename T>
void Foo<T, Bar<T::doStuff() {
std::cout << "Specific" << std::endl;
}

// Not valid:
template <typename T, typename P=Bar<T
void Foo<T, P>::doStuff() {
std::cout << "Specific" << std::endl;
}

// Not valid:
template <typename T, Bar<T
void Foo<T, Bar<T::doStuff() {
std::cout << "Specific" << std::endl;
}

Is there a valid way to achieve this without using a non member template
function and calling that from within the generic doStuff()?

Thanks,
Alan
May 17 '07 #1
4 1343
Alan Woodland wrote:
The following code is legal, and works as expected:

#include <iostream>

template <typename T>
class Bar {
};

template <typename T, typename P>
class Foo {
public:
void doStuff();
};
template <typename T, typename P>
void Foo<T, P>::doStuff() {
std::cout << "General" << std::endl;
}

int main() {
Foo<float, Bar<float a;
a.doStuff();

Foo<float, floatb;
b.doStuff();

return 0;
}
But none of these specialise the method doStuff():

// Not valid:
template <typename T>
void Foo<T, Bar<T::doStuff() {
std::cout << "Specific" << std::endl;
}

// Not valid:
template <typename T, typename P=Bar<T
void Foo<T, P>::doStuff() {
std::cout << "Specific" << std::endl;
}

// Not valid:
template <typename T, Bar<T
void Foo<T, Bar<T::doStuff() {
std::cout << "Specific" << std::endl;
}

Is there a valid way to achieve this without using a non member
template function and calling that from within the generic doStuff()?
No. What you're attempting to do here is to create an _implicit_
partial specialisation of a class template through defining a single
member of it (and without defining the actual partial specialisation
of the class template). That's impossible. You need to define the
entire class template (by repeating most of it) and then define the
member that is different from the unspecialised template.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 17 '07 #2
On May 17, 9:10 am, Alan Woodland <a...@aber.ac.ukwrote:
Hi,

The following code is legal, and works as expected:

#include <iostream>

template <typename T>
class Bar {

};

template <typename T, typename P>
class Foo {
public:
void doStuff();

};

template <typename T, typename P>
void Foo<T, P>::doStuff() {
std::cout << "General" << std::endl;

}

int main() {
Foo<float, Bar<float a;
a.doStuff();

Foo<float, floatb;
b.doStuff();

return 0;

}

But none of these specialise the method doStuff():

// Not valid:
template <typename T>
void Foo<T, Bar<T::doStuff() {
std::cout << "Specific" << std::endl;

}

// Not valid:
template <typename T, typename P=Bar<T
void Foo<T, P>::doStuff() {
std::cout << "Specific" << std::endl;

}

// Not valid:
template <typename T, Bar<T
void Foo<T, Bar<T::doStuff() {
std::cout << "Specific" << std::endl;

}

Is there a valid way to achieve this without using a non member template
function and calling that from within the generic doStuff()?

Thanks,
Alan
You can not specialize the method but you can specialize the class.
Try this.

template <typename T>
class Foo<T,Bar<T {
public:
void doStuff();
};

template <typename T>
void Foo<T, Bar<T::doStuff() {
std::cout << "Specific" << std::endl;

}

May 17 '07 #3
siddhu wrote:
On May 17, 9:10 am, Alan Woodland <a...@aber.ac.ukwrote:
>Hi,

The following code is legal, and works as expected:

#include <iostream>

template <typename T>
class Bar {

};

template <typename T, typename P>
class Foo {
public:
void doStuff();

};

template <typename T, typename P>
void Foo<T, P>::doStuff() {
std::cout << "General" << std::endl;

}

int main() {
Foo<float, Bar<float a;
a.doStuff();

Foo<float, floatb;
b.doStuff();

return 0;

}

But none of these specialise the method doStuff():

// Not valid:
template <typename T>
void Foo<T, Bar<T::doStuff() {
std::cout << "Specific" << std::endl;

}

// Not valid:
template <typename T, typename P=Bar<T
void Foo<T, P>::doStuff() {
std::cout << "Specific" << std::endl;

}

// Not valid:
template <typename T, Bar<T
void Foo<T, Bar<T::doStuff() {
std::cout << "Specific" << std::endl;

}

Is there a valid way to achieve this without using a non member
template function and calling that from within the generic doStuff()?

Thanks,
Alan

You can not specialize the method but you can specialize the class.
Try this.

template <typename T>
class Foo<T,Bar<T {
public:
void doStuff();
};

template <typename T>
void Foo<T, Bar<T::doStuff() {
std::cout << "Specific" << std::endl;

}
This is usually a problem if the need is to [partially] specialise
a single member function out of dozens. Having to specialise the
entire class template means you need to repeat all but one functions
and give them /exactly same/ implementation as the non-specialised
template.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 17 '07 #4
Victor Bazarov wrote:
siddhu wrote:
>On May 17, 9:10 am, Alan Woodland <a...@aber.ac.ukwrote:
[snip]
>>Is there a valid way to achieve this without using a non member
template function and calling that from within the generic doStuff()?

Thanks,
Alan
You can not specialize the method but you can specialize the class.
Try this.

template <typename T>
class Foo<T,Bar<T {
public:
void doStuff();
};

template <typename T>
void Foo<T, Bar<T::doStuff() {
std::cout << "Specific" << std::endl;

}

This is usually a problem if the need is to [partially] specialise
a single member function out of dozens. Having to specialise the
entire class template means you need to repeat all but one functions
and give them /exactly same/ implementation as the non-specialised
template.
For reference the solution I went with in the end was:

#include <iostream>

template <typename T>
class Bar {
};

template <typename T, typename P>
class DoStuffImpl {
public:
static void apply() {
std::cout << "General" << std::endl;
}
};

template <typename T>
class DoStuffImpl<T, Bar<T {
public:
static void apply() {
std::cout << "Specific" << std::endl;
}
};

template <typename T, typename P>
class Foo {
public:
void doStuff();
};
template <typename T, typename P>
void Foo<T, P>::doStuff() {
DoStuffImpl<T, P>::apply();
}

int main() {
Foo<float, Bar<float a;
a.doStuff();

Foo<float, floatb;
b.doStuff();

return 0;
}

Which does what I wanted (i.e. not having to repeat all the other
members) via an additional level of indirection (that probably can be
optimised by a decent compiler anyway)

Thanks,
Alan
May 18 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
5
by: sks_cpp | last post by:
template< class a, class b, class c = int > struct something { }; template< class a, class b > struct something<a, b> { };
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
4
by: Erik Wikström | last post by:
In school (no I will not ask you to do my schoolwork for me) we talked about policy-based design and got an assignment where we got the a code- fragment from a stack-implementation. The idea with...
6
by: rincewind | last post by:
Hi, can anybody summarise all options for partial template specialization, for all kind of parameters (type, nontype, template)? I *think* I understand options for partial specialization on...
1
by: aaragon | last post by:
Hi everyone, I've been trying to compile a very simple code for template partial specializatoin. I can't compile this code! what is wrong with it? I'm using the g++ compiler. #include...
1
by: Martin | last post by:
I'm trying to make a partial specialization of a class of mine. Can someone please tell me what's wrong with the following code? GCC gives me the error "invalid use of undefined type "class X<int,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.