 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
surfintheusa@yahoo.com Guest
|
Posted: Fri Jul 08, 2005 1:14 am Post subject: Can I specialize member functions of a template class? |
|
|
Hello,
Given the following:
template<typename T, size_t D>
class foo{
//...
foo<T, D> bar();
//...
};
template<typename T, size_t D>
foo<T, D> foo<T, D>::bar() { //... }
I'd like to add this:
template<T>
foo<T, 2> foo<T, 2>::bar() { //... }
Is that possible?
MSVC7.1 tells me "error C2244: 'foo<T,D>::bar' : unable to match
function definition to an existing declaration" while pointing to
foo<T, 2>::bar(). But if in my main I instantiate a foo<int, 2>, won't
that declare the foo<int, 2>::bar I want?
I've tried many things to fix/workaround/(please the compiler) but...
no cigar! :(
Thanks for your help!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gareth Guest
|
Posted: Fri Jul 08, 2005 11:13 am Post subject: Re: Can I specialize member functions of a template class? |
|
|
You can't specialise member functions of a template class, but you can
use the following workaround:
#include <iostream>
template<typename T, size_t D>
class foo{
public:
foo<T, D> bar();
};
// Implementation of foo member functions which you want to
// specialise. Note that if these functions need to access non-public
// member data, then foo_impl needs to be a friend of foo.
template<typename T, size_t D>
struct foo_impl {
static foo<T, D> bar(foo<T,D>& f) {
std::cout << "generic" << std::endl;
return foo
}
};
// Specialised implementation for D=2.
template<typename T>
struct foo_impl<T,2> {
static foo<T, 2> bar(foo<T,2>& f) {
std::cout << "D=2" << std::endl;
return foo
}
};
// foo member function defers to implementation structure
template<typename T, size_t D>
foo<T, D> foo<T, D>::bar() { foo_impl<T,D>::bar(*this); }
int main() {
foo<int,1> f1;
f1.bar();
foo<int,2> f2;
f2.bar();
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Fri Jul 08, 2005 3:02 pm Post subject: Re: Can I specialize member functions of a template class? |
|
|
In article <1120816462.429729.66120 (AT) o13g2000cwo (DOT) googlegroups.com>,
Gareth <gareth_stockwell (AT) hotmail (DOT) com> wrote:
| Quote: | You can't specialise member functions of a template class, but you can
use the following workaround:
#include <iostream
template
class foo{
public:
foo
};
// Implementation of foo member functions which you want to
// specialise. Note that if these functions need to access non-public
// member data, then foo_impl needs to be a friend of foo.
template<typename T, size_t D
struct foo_impl {
static foo
std::cout << "generic" << std::endl;
return foo
}
};
// Specialised implementation for D=2.
template<typename T
struct foo_impl
static foo<T, 2> bar(foo<T,2>& f) {
std::cout << "D=2" << std::endl;
return foo
}
};
// foo member function defers to implementation structure
template<typename T, size_t D
foo
int main() {
foo<int,1> f1;
f1.bar();
foo<int,2> f2;
f2.bar();
}
template <class Derived |
struct CRTP
{
void foo() {/* general foo access Derived via a static_cast */}
}
template
class Something:public CRTP<Something
{
friend class CRTP<Something>
//...
};
template <>
struct CRTP<SomeThing
{
void foo() {/* specialization for int_wrapper */}
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
surfintheusa@yahoo.com Guest
|
Posted: Sat Jul 09, 2005 1:37 am Post subject: Re: Can I specialize member functions of a template class? |
|
|
Thanks guys! That did it. :D
Now, I understand how this workaround works but I then have a follow-up
question: what's the good reason behind disallowing that? I have some
guesses about what was going wrong when I tried to compile my faulty
code but can't tell for sure. (If the answer is in a Meyers or Sutter
book, say so, I'll look it up.)
Thanks again!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|