473,387 Members | 1,465 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,387 software developers and data experts.

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>::Bar<int> x;
Foo<int>::Bar<double>::Bar<char*> 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<char *>" 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>::This::Bar<int> x;
Foo<double>::Bar<int> x_clone;

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

// Foo<int>::Bar<double>::Bar<char*> y; // I won't compile on xlC!
Foo<int>::This::Bar<double>::This::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>::Bar<char*>&'
operator
test.cpp:9: candidates are: Foo<int>::Bar<char*>&
Foo<int>::Bar<char*>::operator=(const Foo<int>::Bar<char*>&)

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>::Bar<int> x;
Foo<double>::Bar<int> x_clone;

x = x_clone;

Foo<int>::Bar<double>::Bar<char*> y; // I won't compile on xlC
Foo<int>::Bar<double>::This::Bar<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 1618

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

Similar topics

0
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/ ....
11
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:...
8
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: ...
9
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. ...
6
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...
2
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()...
9
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
21
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...
0
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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,...

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.