[I just posted this to comp.lang.c++.moderated, but I'm impatient]
Hi All,
The simple test program at the end of this message defines a class template
foo with a member function bar implemented by delegating to a static member
function of a specialization of the class template bar_impl, nested within foo.
The template bar_impl is partially specialized outside of foo. My question is
which specialization should be selected: the primary template or the partial
specialization. Or does the program exhibit undefined behavior?
Here are the results for several recent compilers:
* partial specialization is selected - VC7.1-8.0(beta), Comeau 4.3.3, Intel
8.0 for Windows
* primary template is selected: GCC 3.2-3.4.1
* fails to compile: CodeWarrior 8.3-9.2(eval), DigitalMars 8.38n
Best Regards,
Jonathan
----
#include <iostream>
template<typename T>
struct foo {
template<typename U, typename V>
struct bar_impl {
static void bar() { std:: cout << "unspecialized\n"; }
};
void bar()
{
typedef bar_impl<foo<T>, int> type;
type::bar();
}
};
template<typename T>
template<typename V>
struct foo<T>::bar_impl<foo<T>, V> {
static void bar() { std::cout << "specialized\n"; }
};
int main()
{
foo<int> f;
f.bar();
} 2 1186
On Sun, 3 Oct 2004 20:22:16 -0600, "Jonathan Turkanis"
<te******@kangaroologic.com> wrote: [I just posted this to comp.lang.c++.moderated, but I'm impatient]
Hi All,
The simple test program at the end of this message defines a class template foo with a member function bar implemented by delegating to a static member function of a specialization of the class template bar_impl, nested within foo. The template bar_impl is partially specialized outside of foo. My question is which specialization should be selected: the primary template or the partial specialization. Or does the program exhibit undefined behavior?
Here are the results for several recent compilers:
* partial specialization is selected - VC7.1-8.0(beta), Comeau 4.3.3, Intel 8.0 for Windows
I think they're right.
* primary template is selected: GCC 3.2-3.4.1
* fails to compile: CodeWarrior 8.3-9.2(eval), DigitalMars 8.38n
Best Regards, Jonathan
----
#include <iostream>
template<typename T> struct foo { template<typename U, typename V> struct bar_impl { static void bar() { std:: cout << "unspecialized\n"; } };
void bar() { typedef bar_impl<foo<T>, int> type; type::bar();
The above doesn't cause an implicit instantiation, since T isn't known
- "type" is dependent.
} };
template<typename T> template<typename V> struct foo<T>::bar_impl<foo<T>, V> { static void bar() { std::cout << "specialized\n"; } };
int main() { foo<int> f;
The above is the first use of the partial specialization that would
cause implicit instantiation to occur. The partial specialization has
been declared already.
f.bar(); }
By 14.5.4/1 it looks like it should compile and work as it does with
VC and EDG.
Tom
"Tom Widmer" <to********@hotmail.com> wrote in message
news:so********************************@4ax.com... On Sun, 3 Oct 2004 20:22:16 -0600, "Jonathan Turkanis" <te******@kangaroologic.com> wrote:
[I just posted this to comp.lang.c++.moderated, but I'm impatient]
Hi All,
The simple test program at the end of this message defines a class
templatefoo with a member function bar implemented by delegating to a static member function of a specialization of the class template bar_impl, nested within
foo.The template bar_impl is partially specialized outside of foo. My question is which specialization should be selected: the primary template or the partial specialization. Or does the program exhibit undefined behavior?
Here are the results for several recent compilers:
* partial specialization is selected - VC7.1-8.0(beta), Comeau 4.3.3, Intel 8.0 for Windows
I think they're right.
Thanks. That's what I was hoping.
Jonathan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dave Theese |
last post by:
Hello all,
I'm trying to get a grasp of the difference between specializing a function
template and overloading it. The example below has a primary template, a
specialization and an overload. ...
|
by: jesse |
last post by:
I am frustrated by class specialization. i don't
think it helps me a lot.
suppose we have
template <class T>
class Talkative
{
T& t;
public:
|
by: Agent Mulder |
last post by:
Hi group,
I have a problem with partial template specialization. In the code
below I have a template struct Music with one method, play(),
and three kinds of music, Jazz, Funk and Bach. When I...
|
by: TT \(Tom Tempelaere\) |
last post by:
Comeau compiler complains (too few arguments for class template "B") at line
***
#include <memory>
template<typename T, size_t n>
struct A {};
template<typename T, size_t n>
struct B;
|
by: Michael Sparks |
last post by:
I started writing some code with the 2.0 beta compiler, and found out
quickly that specialization is not supported with generics.
For example, I want to do something like this:
class number...
|
by: Yang Zhang |
last post by:
I have a small program like this:
////////////////////////////////////////////////
#include <iostream>
using namespace std ;
// set bits in an address
template <typename T>
inline T*...
|
by: Imre |
last post by:
Hi
I'd like to know what the problem is with the following code. I've
tried compiling it with two compilers (VC++ 8.0, and Comeau online
compiler), and both failed to compile it, saying that...
|
by: desktop |
last post by:
I have this example:
template<class T(1)
void f( T );
template<class T(2)
void f( T* );
template< (3)
|
by: mike b |
last post by:
Hello everyone, thanks in advance for your help. I'm new to C++
templates and have run into some issues using member function
templates. I have a shared library containing templates that I'm...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |