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

Cannot invoke a templated ctor

The following program produces error at the definition of ``b''. However it
compiles fine the second (supposedly equivalent) form of the templated
constructor. Why the two alternatives aren't equivalent ?

template<class Obj>
struct ptr_to_member
{
typedef void (Obj::* type) ();
};

struct A
{
A (void (*fn) ())
{
}

#if 1
template<class Obj>
A (typename ptr_to_member <Obj>::type fn)
{
}
#else
template<class Obj>
A (void (Obj::* fn) ())
{
}
#endif
};

void foo ()
{
}

struct bar
{
void baz ()
{
}
};

A a (foo);

A b (&bar::baz);
Jul 19 '05 #1
10 2095
"Momchil Velikov" <ve***@fadata.bg> wrote in message
news:87**************************@posting.google.c om...
[...]
template<class Obj>
A (typename ptr_to_member <Obj>::type fn)
{
}
This is a non-deducible context.
template<class Obj>
A (void (Obj::* fn) ())
{
}


This context is deducible.

Basically, if the compiler has to instantiate a type to match a
template parameter, then the context is non-deducible (the
compiler is too lazy to do the instantiation, since it could
become arbitrarily complex, and probably lead to weird
recursions in some cases).

Dave
Jul 19 '05 #2

"David B. Held" <dh***@codelogicconsulting.com> wrote in message
news:bk**********@news.astound.net...
"Momchil Velikov" <ve***@fadata.bg> wrote in message
news:87**************************@posting.google.c om...
[...]
template<class Obj>
A (typename ptr_to_member <Obj>::type fn)
{
}


This is a non-deducible context.
template<class Obj>
A (void (Obj::* fn) ())
{
}


This context is deducible.

Basically, if the compiler has to instantiate a type to match a
template parameter, then the context is non-deducible (the
compiler is too lazy to do the instantiation, since it could
become arbitrarily complex, and probably lead to weird
recursions in some cases).

Dave


[Just a comment]
Interestingly enough Visual C++ 6.0 (SP5) which is not yet a very standard
compliant compiler in terms of templates, manages to deduce the context.

Chris
Jul 19 '05 #3
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:bk**********@sunnews.cern.ch...
[...]
[Just a comment]
Interestingly enough Visual C++ 6.0 (SP5) which is not yet a very
standard compliant compiler in terms of templates, manages to
deduce the context.


Interesting. Please try this code on it:

template <typename T>
struct identity
{
typedef T type;
};

template <typename T>
void foo(typename identity<T>::type)
{
}

int main()
{
int i;
foo(i);
}

Comeau online gives:

"ComeauTest.c", line 15: error: no instance of function template "foo"
matches the argument list
The argument types that you used are: (int)
foo(i);
^

Note that the identity metafunction is often used explicitly to
suppress deduction, so VC6 is truly broken indeed if it performs
the deduction anyway (but I have a suspicion that it doesn't,
and something else is going on with your test code).

Dave
Jul 19 '05 #4

"David B. Held" <dh***@codelogicconsulting.com> wrote in message
news:bk**********@news.astound.net...
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:bk**********@sunnews.cern.ch...
[...]
[Just a comment]
Interestingly enough Visual C++ 6.0 (SP5) which is not yet a very
standard compliant compiler in terms of templates, manages to
deduce the context.
Interesting. Please try this code on it:

template <typename T>
struct identity
{
typedef T type;
};

template <typename T>
void foo(typename identity<T>::type)
{
}

int main()
{
int i;
foo(i);
}

Comeau online gives:

"ComeauTest.c", line 15: error: no instance of function template "foo"
matches the argument list
The argument types that you used are: (int)
foo(i);
^


And IMHO it's right.

Note that the identity metafunction is often used explicitly to
suppress deduction, so VC6 is truly broken indeed if it performs
the deduction anyway (but I have a suspicion that it doesn't,
and something else is going on with your test code).

Dave


VC6 compiles the code just fine. Probably I'll give it a try on VC7
sometime.

Chris
Jul 19 '05 #5

"David B. Held" <dh***@codelogicconsulting.com> wrote in message
news:bk**********@news.astound.net...
"Momchil Velikov" <ve***@fadata.bg> wrote in message
news:87**************************@posting.google.c om...
[...]
template<class Obj>
A (typename ptr_to_member <Obj>::type fn)
{
}


This is a non-deducible context.
template<class Obj>
A (void (Obj::* fn) ())
{
}


This context is deducible.

Basically, if the compiler has to instantiate a type to match a
template parameter, then the context is non-deducible (the
compiler is too lazy to do the instantiation, since it could
become arbitrarily complex, and probably lead to weird
recursions in some cases).

Dave


Hi Dave, can you give me the exact part of the standard for this, please.

Regards
Chris
Jul 19 '05 #6
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message news:<bk**********@sunnews.cern.ch>...
VC6 compiles the code just fine. Probably I'll give it a try on VC7
sometime.


FWIW, VC++.NET (aka VC++7) does not accept it. But since VC++6 accepts
it, I don't know why ISO C++ shouldn't also accept it. That is, I
figured it just happened to be difficult or impossible to do, but
VC++6 did it.

Sounds like a topic for comp.std.c++, actually.

- Shane
Jul 19 '05 #7
In article <2f**************************@posting.google.com >,
sb******@cs.uic.edu says...

[ ... ]
FWIW, VC++.NET (aka VC++7) does not accept it. But since VC++6 accepts
it, I don't know why ISO C++ shouldn't also accept it. That is, I
figured it just happened to be difficult or impossible to do, but
VC++6 did it.


Off the top of my head, I don't remember exactly what you're talking
about, but I guess it doesn't really matter. VC++ 6 simplifies the
language to the point that quite a few ambiguities in the real language
don't exist in its language. An ambiguity results when a particular
expression _could_ be interpreted in one of two possible ways. When/if
the compiler is simply limited to the point that it doesn't recognize
one of those possibilities at all, then what should be ambiguous input
is unambiguous for it. Making the compiler recognize the other
possibility, however, makes the expression ambiguous.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #8
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message
news:bk**********@sunnews.cern.ch...
[...]
Hi Dave, can you give me the exact part of the standard for this,
please.


I wish I could, by my only copy of the standard is on a dead HD,
so I'll have to give you the exact part of the draft standard instead:

14.8.2/9

14.8.2/10:

In a type which contains a nested-name-specifier, template
argument values cannot be deduced for template parameters
used within the nested-name-specifier. [Example:

template<int i, typename T>
T deduce(A<T>::X x, // T is not deduced here
T t, // but T is deduced here
B<i>::Y y); // i is not deduced here

I think paragraph 10 pretty much sums it up (it does for me,
anyway).

I also highly recommend the Vandevoorde/Josuttis text on
templates. It doesn't always make things clear, but it always
makes things precise, and since templates sometimes can't be
clear with any amount of explanation, precision is as good as
it gets. ;>

Dave
Jul 19 '05 #9
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message news:<bk**********@sunnews.cern.ch>...
"David B. Held" <dh***@codelogicconsulting.com> wrote in message
news:bk**********@news.astound.net...
"Momchil Velikov" <ve***@fadata.bg> wrote in message
news:87**************************@posting.google.c om...
[...]
template<class Obj>
A (typename ptr_to_member <Obj>::type fn)
{
}


This is a non-deducible context.
template<class Obj>
A (void (Obj::* fn) ())
{
}


This context is deducible.

Hi Dave, can you give me the exact part of the standard for this, please.


14.8.2.4 [#4]

~velco
Jul 19 '05 #10
Jerry Coffin <jc*****@taeus.com> wrote in message news:<MP************************@news.clspco.adelp hia.net>...
VC++ 6 simplifies the
language to the point that quite a few ambiguities in the real language
don't exist in its language.


Actually, you're right. It seems that this "trick" only works in the
case where the function parameter is something like

typename foo<T>::type

where foo<T>::type is dependent on T. Frankly, that is rather useless.

Nevermind. :)

- Shane
Jul 19 '05 #11

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

Similar topics

3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
1
by: Rich | last post by:
Hi, I have a query regarding VC6 and its handling of templated copy constructors. Here goes: Take a look at the following code sample... template<class _Ty, size_t t_uiSize = 10 > class...
2
by: John Harrison | last post by:
I want to write a templated constructor with a non-type template argument, like this. class X { public: X() : val(0) {} template <int I> X(X const&, X const&) : val(I) {}
2
by: ferdinand.stefanus | last post by:
Hi, I have some questions regarding templated class constructor: #include <iostream> using namespace std; template<typename T> class Foo { public:
5
by: kalita | last post by:
template<class T> class A { template<class Y> A(const A<Y> &) { // whatever } };
3
by: Subbu | last post by:
I have created a simple ASP.NET web server. I expose a methos helloWorld(). when I invoke this method I receive the following error message. I definitely seems like a setting issue. Would really...
12
by: Robert.Holic | last post by:
Hi All (first time caller, long time listener), I've stumbled across a problem that I have yet to figure out, although Im sure I'll kick myself when I figure it out. Here it is: I need to...
6
by: Richard Thompson | last post by:
Hi - I have a program which was previously working (but wasn't well tested). I've added a new function call, and it now doesn't compile. The call requires a copy constructor, but the compiler...
2
by: Olaf | last post by:
Hi, I'm working on a thin libcurl layer. Therefore I have the following classes: template<typename T, long CURLOPT_ID> class CurlOption: boost::noncopyable { public: typedef typename...
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: 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
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
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.