473,790 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Curious template behavior, g++ and xlC and icpc

Hi,

Consider this code fragment, based on the article "Using Chains to Free
Library Code" from the July 2005 issue of C/C++ Users Journal:

-- begin listing --
template <class T>
struct Foo
{
template <class S>
struct Bar : public Foo<S>
{
};
};

int main()
{
Foo<double>::Ba r<int> x;
Foo<int>::Bar<d ouble>::Bar<cha r*> y;

return 0;
}
-- end listing --

g++ and icpc compiles this bit of code without complaint. xlC, on the
other hand, has trouble with resolving the names and returns the error:

"test.cpp", line 14.39: 1540-0300 (S) The "private" member "struct
Foo<int>::Bar<c har *>" cannot be accessed.

This leads me to the first question (since I'm not as well versed in
the standard as I would like): which compiler is to be believed? And a
second question: how can I write a portable version of that listing? I
took a stab at the second question and came up with:

-- begin listing --
template <class T>
struct Foo
{
typedef Foo<T> This;

template <class S>
struct Bar : public Foo<S>
{
};
};

int main()
{
Foo<double>::Th is::Bar<int> x;
Foo<double>::Ba r<int> x_clone;

// A quick test to see if the types make sense
x = x_clone;

// Foo<int>::Bar<d ouble>::Bar<cha r*> y; // I won't compile on xlC!
Foo<int>::This: :Bar<double>::T his::Bar<char*> y_clone;

// y = y_clone; // I won't compile on xlC!

return 0;
}
-- end listing --

At first glance, this seems to do the trick, but if we uncomment the "I
won't compile" lines, and try to compile them with g++ we get:

test.cpp: In function `int main()':
test.cpp:23: no match for `Foo<int>::Bar< char*>& =
Foo<double>::Ba r<char*>&'
operator
test.cpp:9: candidates are: Foo<int>::Bar<c har*>&
Foo<int>::Bar<c har*>::operator =(const Foo<int>::Bar<c har*>&)

icpc gives a similar error. The types aren't matching up! The typedef
is looking at the top level for y, but at the inherited level for
y_clone. So what's a solution to this? Moving the typedef from the
parent class to the child class gives us:

-- begin listing --
template <class T>
struct Foo
{

template <class S>
struct Bar : public Foo<S>
{
typedef Foo<T> This;
};
};

int main()
{
Foo<double>::Ba r<int> x;
Foo<double>::Ba r<int> x_clone;

x = x_clone;

Foo<int>::Bar<d ouble>::Bar<cha r*> y; // I won't compile on xlC
Foo<int>::Bar<d ouble>::This::B ar<char*> y_clone;

y = y_clone; // I won't compile on xlC

return 0;
}
-- end listing --

We get the desired behavior with icpc and g++ (that is, the type
signatures match up), and xlC builds the program. Now, since building
is not a sign of correctness, I will state that the desired
funcionality (as described in the article) is obtained, at the expense
of the original goal of brevity. My inclination is to believe that the
xlC implementation of templates is broken. Anyone have thoughts or
comments?

Thanks,
Chris

Jul 23 '05 #1
0 1635

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

Similar topics

0
2096
by: Jerome Lefebvre | last post by:
Hello, Hope this will interest a few. I been working with a friend on the problems given out during the "International Collegiate Programming Contest" (ICPC) http://icpc.baylor.edu/icpc/ . Started out just trying to find the solutions and then moving on, to "aggressively" trying to find the best solution for each problems. First started to work on the problems using C++ and then moved on to using Python, mostly since it was much easier...
11
3627
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected: virtual void do_bar() {} };
8
1881
by: kevin | last post by:
Hello! So I was reading O'Reilly's C++ in a Nutshell when I came accross something interesting: The class definition for basic_ostream contains numerous overloaded operator<< functions: template <class charT, class traits = char_traits<charT class basic_ostream : virtual public basic_ios<charT,traits>
9
3472
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. //------------------------------------------------------------------ // my regular glass class A { }; // my templated class
6
1334
by: Kai-Uwe Bux | last post by:
Juha Nieminen wrote: I think your code violates the One-Definition-Rule. The template function foo() is defined in two significantly different ways in the two files. As far as I know, no diagnostic is required for such errors.
2
1645
by: Juha Nieminen | last post by:
Paavo Helde wrote: So which compiler is correct, gcc (which only requires bar() to be declared at the point of insantiation of foo()) or comeau? What happens if you just move the bar() function to be before the foo() function in both source files? (It's not like this was critical from the point of view of my original post.)
9
2065
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
21
4748
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code that gets the same result) works as expected in visualc++. I know that this is probably not the right behavior for a compiler but it's the kind of behavior I'm searching for so I was hoping there was a way to do the same thing in gcc. As you know...
0
1673
by: Biswajyoti Pal | last post by:
IIT Kharagpur KSHITIJ 2009 THE ANNUAL TECHNO-MANAGEMENT FEST 29th Jan- 1st Feb THE OVERNITE ACM ICPC 2009 MULTI PROVINCIAL PROGRAMMING CONTEST ( http://overnite.ktj.in )
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10413
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, 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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10145
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7530
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2909
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.