Hi,
I would like to specify behavior of a class member relatively to
template implemetation. It works in usual cases but it seems to fail
with to templates when one of the two is specified...
Exemple:
template <class T, int Num>
class A
{
void foo ();
}
// The generic implementation is:
template <class T, int Num>
void
A<T,Num>
::foo ()
{
// generic code
}
// I would like to add a specific code when Num equals 2 fior
instance:
template <class T>
void
A<T,2>
::foo()
{
// an other code...
}
// But g++ 3.2.3 (on Redhat Fedora 5) does not like it
Is there any solution, knowing that a complete non-genric code is
possible (i.e. defined a void A<int,2>::foo( ) directly)?
Any help is appreciated.
Greg 9 1942
On 16 Srp, 11:53, Greg <Gregoire.M.Mer c...@gmail.comw rote:
Hi,
I would like to specify behavior of a class member relatively to
template implemetation. It works in usual cases but it seems to fail
with to templates when one of the two is specified...
Exemple:
template <class T, int Num>
class A
{
void foo ();
}
// The generic implementation is:
template <class T, int Num>
void
A<T,Num>
::foo ()
{
// generic code
}
// I would like to add a specific code when Num equals 2 fior
instance:
template <class T>
void
A<T,2>
::foo()
{
// an other code...
}
// But g++ 3.2.3 (on Redhat Fedora 5) does not like it
Is there any solution, knowing that a complete non-genric code is
possible (i.e. defined a void A<int,2>::foo( ) directly)?
Any help is appreciated.
Greg
Partial specialization works only for classes/structs:
template <class T, int Num>
class A
{
void foo ()
{
// generic code
}
}
template <class T>
class A<T, 2>
{
void foo ()
{
// specialized code
}
}
Hi!
Ondra Holub schrieb:
Partial specialization works only for classes/structs:
And for non-member functions and template functions of non-template
classes. Right?
They just don't work for partially specializing a templated member
function of a class template. Right?
Is there a link to read up on this issue?
Frank
On 16 Srp, 14:00, Frank Birbacher <bloodymir.c... @gmx.netwrote:
Hi!
Ondra Holub schrieb:
Partial specialization works only for classes/structs:
And for non-member functions and template functions of non-template
classes. Right?
I think it works only for classes (and structs, what is the same with
different default access type). You can as workaround create class
with static method and use partial specialization of this class.
AFAIK partial specialization of functions is considered in prepared
standard, but I am not sure.
Hi!
Ondra Holub schrieb:
AFAIK partial specialization of functions is considered in prepared
standard, but I am not sure.
No. The following already works (not with MSVC prior to 7.1, though):
//generic
template<typena me T>
void process(T t) {}
//partial specialization
template<typena me T>
void process(T* const t) {}
//full specialization
template<>
void process(void* const t) { throw "what is it?"; }
int main()
{
int i = 8;
process(i);
process(&i);
process( static_cast<voi d*>(&i) );
}
Frank
This also seems to work in VC8 anyway and has the advantage of not
having to reimplement the whole of class A:
#include <iostream>
template <class T, int Num>
class A
{
public:
void foo()
{
foo_impl<Num>() ;
}
private:
template <int N>
void foo_impl()
{
std::cout << "Default Foo" << std::endl;
}
template <>
void foo_impl<2>()
{
std::cout << "Special Foo" << std::endl;
}
};
int main()
{
A<int, 0a0;
A<int, 2a2;
a0.foo();
a2.foo();
}
Hope that helps in some way.
joe
On 16 Srp, 14:48, Frank Birbacher <bloodymir.c... @gmx.netwrote:
Hi!
Ondra Holub schrieb:
AFAIK partial specialization of functions is considered in prepared
standard, but I am not sure.
No. The following already works (not with MSVC prior to 7.1, though):
//generic
template<typena me T>
void process(T t) {}
//partial specialization
template<typena me T>
void process(T* const t) {}
//full specialization
template<>
void process(void* const t) { throw "what is it?"; }
int main()
{
int i = 8;
process(i);
process(&i);
process( static_cast<voi d*>(&i) );
}
Frank
Yes. it works. But it is full specialization, not partial
specialization. Try following:
template<typena me T, int N>
void Func()
{
}
template<typena me T>
void Func<T, 10>()
{
}
On gcc you'll get error: partial specialization `Func<T, 10>' of
function template, on Visual C++ .net 2003 you'll get error C2768:
'Func' : illegal use of explicit template arguments (the same on
Visual C++ 2005).
>
Hi!
Ondra Holub schrieb:
AFAIK partial specialization of functions is considered in prepared
standard, but I am not sure.
No. The following already works (not with MSVC prior to 7.1, though):
//generic
template<typena me T>
void process(T t) {}
//partial specialization
template<typena me T>
void process(T* const t) {}
//full specialization
template<>
void process(void* const t) { throw "what is it?"; }
int main()
{
int i = 8;
process(i);
process(&i);
process( static_cast<voi d*>(&i) );
}
Frank
Yes. it works. But it is full specialization, not partial
specialization. Try following:
No it's just a overloaded template function.the last one is a full
specialization though
Hi!
Ondra Holub schrieb:
On 16 Srp, 14:48, Frank Birbacher <bloodymir.c... @gmx.netwrote:
> //generic template<typen ame T> void process(T t) {}
//partial specialization template<typen ame T> void process(T* const t) {}
Is the above really a "full specialization" ? o_O
Yes. it works. But it is full specialization, not partial
specialization. Try following:
template<typena me T, int N>
void Func()
{
}
template<typena me T>
void Func<T, 10>()
{
}
-.- ok, fails. But why? I don't understand this restriction, especially
because classes can be partially specialized.
Frank
On 16 Srp, 16:04, Frank Birbacher <bloodymir.c... @gmx.netwrote:
Hi!
Ondra Holub schrieb:
On 16 Srp, 14:48, Frank Birbacher <bloodymir.c... @gmx.netwrote:
//generic
template<typena me T>
void process(T t) {}
//partial specialization
template<typena me T>
void process(T* const t) {}
Is the above really a "full specialization" ? o_O
Well, you're right, I missed this one in the middle :-)
>
Yes. it works. But it is full specialization, not partial
specialization. Try following:
template<typena me T, int N>
void Func()
{
}
template<typena me T>
void Func<T, 10>()
{
}
-.- ok, fails. But why? I don't understand this restriction, especially
because classes can be partially specialized.
Frank
I do not understand it too, but this is current situation :-( I
usually use workaround with static class member which works, although
it looks not as good as simple function:
template<typena me T, int N>
class X
{
static void Func() { }
};
template<typena me T>
class X<T, 10>
{
void Func<T, 10>() { }
};
Maybe we'll have to wait for a new standard... And then it will depend
on supplier of our compiler :-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 18.3.1, 'Iseq'). I want the version for containers that he
gives, but also to provide a specialization for construction from a
pair<It,It> (eg because...
|
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
|
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>
|
by: Kai-Uwe Bux |
last post by:
Hi folks,
I observed something that puzzles me. When I do
namespace xxx { using std::swap; }
it appears that xxx::swap and std::swap are not strictly equivalent. In
particular, I think that my implementation will only choose the partial
specialization for std::vector when std::swap is used.
|
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 the code was that
if, when its destructor was called, it still contained any elements it
would run 'delete' on them if they were pointers and do...
| |
by: Gomaw Beoyr |
last post by:
Two question about the "partial classes" (in the next wersion of
..NET).
Question 1
==========
Will partial classes (in the next version of C#) have to be
declared "partial" in ALL places.
I.e. do we have to need to write:
|
by: wakun |
last post by:
Hi there,
I am learning template programming. When testing the partial
specialization, I have some probelms
Here is a full templated class
template <typename T, int n>
class CT
{
public:
T data;
|
by: wkaras |
last post by:
I tried a couple of compilers, and both gave errors compiling this:
template <bool fin, typename T>
T foo(T val);
template <typename T>
T foo<true, T>(T val) { return(val); }
But both gave no errors compiling this:
|
by: Marek Vondrak |
last post by:
Hello.
I have written the following program and am curious why it prints "1" "2".
What are the exact effects of explicitly providing function template
parameters at the call? Is the second assign() function really a
specialization of the first assign() or is it an assign() overload?
Thank you.
-- Marek
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |