473,406 Members | 2,312 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,406 software developers and data experts.

function template overloading and typedef in GCC

Hi all,

I am having a problem trying to overload a function template, based on
a typedef, such as:

template<class T>
struct A
{};

template<class T>
struct B
{
typedef A<T> type;
};

template<class T>
void foo(const typename B<T>::type&)
{}

void bar()
{
foo(B<int>::type());
}

g++ 3.3.1 complains at the above code with the following message:
no matching function for call to `foo(A<int>)'

Similar code works with no problem in VC6.

Am I missing something?

Thanks in advance,

Arkadiy
Jul 19 '05 #1
5 7082
Arkadiy Vertleyb wrote:
Hi all,

I am having a problem trying to overload a function template, based on
a typedef, such as:

template<class T>
struct A
{};

template<class T>
struct B
{
typedef A<T> type;
};

template<class T>
void foo(const typename B<T>::type&)
{}

void bar()
{
foo(B<int>::type());
}

g++ 3.3.1 complains at the above code with the following message:
no matching function for call to `foo(A<int>)'

Similar code works with no problem in VC6.

Am I missing something?


Even if this is not allowed by the standard (which I think it is given
some things I've seen - just a hunch) the error message is totally wrong.

Besides:

foo<int>(B<int>::type());

compiles fine.

I suggest you post a bug on gcc. Before you do, you might want to try a
gcc 3.4 devel snapshot. Some template things (like this) have been
fixed in the development branch.

Jul 19 '05 #2
On 25 Sep 2003 19:27:16 -0700, ve******@hotmail.com (Arkadiy Vertleyb)
wrote:
Hi all,

I am having a problem trying to overload a function template, based on
a typedef, such as:

template<class T>
struct A
{};

template<class T>
struct B
{
typedef A<T> type;
};

template<class T>
void foo(const typename B<T>::type&)
The above contains a non-deducable context (in this case, a nested
type). This means that calls to the function cannot rely on template
argument deduction, but must use explicit template argument
specification.

The reason this is non-deducable is that the mapping from nested type
to containing type is not in general one-to-one. Often many different
Ts would give the same B<T>::type (think specialization of B).
{}

void bar()
{
foo(B<int>::type());
foo<int>(B<int>::type());
} g++ 3.3.1 complains at the above code with the following message:
no matching function for call to `foo(A<int>)'
Right.
Similar code works with no problem in VC6.
Yes, this is a bizarre "feature" of VC6 that it deduces this kind of
non-deducable context.
Am I missing something?


Just that VC6 is very old and non-standard.

Tom
Jul 19 '05 #3
tom_usenet wrote:
On 25 Sep 2003 19:27:16 -0700, ve******@hotmail.com (Arkadiy Vertleyb)
wrote:

Hi all,

I am having a problem trying to overload a function template, based on
a typedef, such as:

template<class T>
struct A
{};

template<class T>
struct B
{
typedef A<T> type;
};

template<class T>
void foo(const typename B<T>::type&)

The above contains a non-deducable context (in this case, a nested
type).

What's non-deductible about it ?

const typename B<T>::type& <<>> B<int>::type

infers T is int. right ?

How does the standard define non-deductible ?

I must admit, I've never come across this kind of problem, so I really
don't know. Time to brush up ...
Jul 19 '05 #4
On 26 Sep 2003 14:09:31 GMT, Gianni Mariani <gi*******@mariani.ws>
wrote:
tom_usenet wrote:
On 25 Sep 2003 19:27:16 -0700, ve******@hotmail.com (Arkadiy Vertleyb)
wrote:

Hi all,

I am having a problem trying to overload a function template, based on
a typedef, such as:

template<class T>
struct A
{};

template<class T>
struct B
{
typedef A<T> type;
};

template<class T>
void foo(const typename B<T>::type&)

The above contains a non-deducable context (in this case, a nested
type).

What's non-deductible about it ?


It attempts to deduce a template parameter from the type of a typedef
of the template. 14.8.2.4/4 is the relevent bit of the standard.
const typename B<T>::type& <<>> B<int>::type

infers T is int. right ?
No, and the deduction can't even reliably be made. e.g.

template<>
struct B<float>
{
typedef A<int> type; //whoops, now which B?
};

Of course, this could just cause an ambiguity error, as it does in
VC6. However, the mapping from inner type to template parameter is not
always many-to-one, so the committee decided to forbid it.

How does the standard define non-deductible ?


Read up on nondeduced contexts.

Tom
Jul 19 '05 #5
Hi guys,

Thanks for your responce.

Come to think about it, I do realize that overloading on a nested
typedef in this context (at least in my case) doesn't make a lot of
sense. The nested typedef may after all resolve to different types,
like:

template<class T>
struct A1
{};

template<class T>
struct A2
{};

template<class T>
struct B
{
// something like:
typedef select<some_condition_based_on_T, A1<T>, A2<T> >::type type;
};

For both A1 and A2 I need to define a separate overloaded function.
So I actually have to overload on A1<T> and A2<T>, rather than on
B<T>::type.

Regards,

Arkadiy
tom_usenet <to********@hotmail.com> wrote in message news:<9j********************************@4ax.com>. ..
On 26 Sep 2003 14:09:31 GMT, Gianni Mariani <gi*******@mariani.ws>
wrote:
tom_usenet wrote:
On 25 Sep 2003 19:27:16 -0700, ve******@hotmail.com (Arkadiy Vertleyb)
wrote:
Hi all,

I am having a problem trying to overload a function template, based on
a typedef, such as:

template<class T>
struct A
{};

template<class T>
struct B
{
typedef A<T> type;
};

template<class T>
void foo(const typename B<T>::type&)
The above contains a non-deducable context (in this case, a nested
type).

What's non-deductible about it ?


It attempts to deduce a template parameter from the type of a typedef
of the template. 14.8.2.4/4 is the relevent bit of the standard.
const typename B<T>::type& <<>> B<int>::type

infers T is int. right ?


No, and the deduction can't even reliably be made. e.g.

template<>
struct B<float>
{
typedef A<int> type; //whoops, now which B?
};

Of course, this could just cause an ambiguity error, as it does in
VC6. However, the mapping from inner type to template parameter is not
always many-to-one, so the committee decided to forbid it.

How does the standard define non-deductible ?


Read up on nondeduced contexts.

Tom

Jul 19 '05 #6

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

Similar topics

2
by: Ivan | last post by:
How (at compile time) can one determine whether some class implements a particular member function? Name only is sufficient, full signature match nice but not required. The application is a...
4
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. ...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
11
by: gao_bolin | last post by:
I am facing the following scenario: I have a class 'A', that implements some concept C -- but we know this, not because A inherits from a virtual class 'C', but only because a trait tell us so: ...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
2
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last...
4
by: Dan Krantz | last post by:
I have the following template to ensure that a given number (val) falls into a range (between vmin & vmax): template<typename T> T ForceNumericRange( const T& val, const T& vmin, const T& vmax)...
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
0
by: Gordon Schumacher | last post by:
I found a clever way to solve this issue: I used a variant of the code at http://www.flipcode.com/cgi-bin/fcarticles.cgi?show=64171. On one hand, this code is fairly arcane, and I realize that...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
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...
0
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...
0
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,...

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.