 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Patrick Kowalzick Guest
|
Posted: Fri Oct 29, 2004 11:23 am Post subject: Problems with ....hmm, linker perhaps |
|
|
Dear all,
IMO the following program is legal and should compile.
//***** code start *********
// Definition of class A
template <typename T> struct A
{
typedef T value_type;
};
// Definition of class B
template <typename> struct B {};
// Function foo
// returntype important !
template <typename T>
B<typename T::value_type> foo()
{
return B<typename T::value_type>();
};
int main()
{
typedef B< int >( * ptr )(); // Function pointer
ptr foo_ptr = foo< A; // Initialization of function pointer
return 0;
}
//***** code end *********
It builds fine with g++ and MSVC7.1, but the latter one fails linking:
unresolved external symbol "struct B<int> __cdecl foo<struct A(void)"
(??$foo@U?$A@H@@@@YA?AU?$B@H@@XZ) referenced in function _main
what I do not really understand. It is getting a lit more weird now. If I
change
ptr foo_ptr = foo< A;
to
ptr foo_ptr ;
foo_ptr = foo< A;
it compiles and links fine as well with MSVC 7.1.
Before I blame anyone, I want to ask if I overlooked something? IMO the code
is fine.
Regards,
Patrick
|
|
| Back to top |
|
 |
JKop Guest
|
Posted: Fri Oct 29, 2004 11:56 am Post subject: Re: Problems with ....hmm, linker perhaps |
|
|
| Quote: | If I change
ptr foo_ptr = foo< A;
to
ptr foo_ptr ;
foo_ptr = foo< A;
it compiles and links fine as well with MSVC 7.1.
|
Well, without scrutanizing your code too much, the only difference in the
above is that the former calls a constructor, while the latter calls the
default constructor and then calls operator=.
-JKop
|
|
| Back to top |
|
 |
Thomas Maier-Komor Guest
|
Posted: Fri Oct 29, 2004 11:57 am Post subject: Re: Problems with ....hmm, linker perhaps |
|
|
Patrick Kowalzick wrote:
| Quote: | Dear all,
IMO the following program is legal and should compile.
//***** code start *********
// Definition of class A
template <typename T> struct A
{
typedef T value_type;
};
// Definition of class B
template <typename> struct B {};
// Function foo
// returntype important !
template <typename T
B
{
return B<typename T::value_type>();
};
int main()
{
typedef B< int >( * ptr )(); // Function pointer
ptr foo_ptr = foo< A; // Initialization of function pointer
return 0;
}
//***** code end *********
It builds fine with g++ and MSVC7.1, but the latter one fails linking:
unresolved external symbol "struct B<int> __cdecl foo<struct A(void)"
(??$foo@U?$A@H@@@@YA?AU?$B@H@@XZ) referenced in function _main
what I do not really understand. It is getting a lit more weird now. If I
change
ptr foo_ptr = foo< A;
to
ptr foo_ptr ;
foo_ptr = foo< A;
it compiles and links fine as well with MSVC 7.1.
Before I blame anyone, I want to ask if I overlooked something? IMO the code
is fine.
Regards,
Patrick
|
compiles and links fine with Sun Studio 9...
|
|
| Back to top |
|
 |
Patrick Kowalzick Guest
|
Posted: Fri Oct 29, 2004 12:02 pm Post subject: Re: Problems with ....hmm, linker perhaps |
|
|
| Quote: | compiles and links fine with Sun Studio 9...
|
Thx.
|
|
| Back to top |
|
 |
Unforgiven Guest
|
Posted: Fri Oct 29, 2004 12:06 pm Post subject: Re: Problems with ....hmm, linker perhaps |
|
|
"Patrick Kowalzick" <Patrick.Kowalzick (AT) cern (DOT) ch> wrote
| Quote: | Dear all,
IMO the following program is legal and should compile.
|
I concur with your opinion. I tested and verified that VC7.1 does not. I
think it's not a problem with the linker though, it looks as if the compiler
isn't instantiating the foo<A template, so when the linker looks for
it, it finds nothing.
I also tested this under Visual C++ 2005 (VC8) beta 1
([url]http://lab.msdn.microsoft.com/vs2005/)[/url], and it compiles and links fine,
which seems to suggest this is indeed a bug in VC7.1 which has been fixed
for VC8.
--
Unforgiven
|
|
| Back to top |
|
 |
Patrick Kowalzick Guest
|
Posted: Fri Oct 29, 2004 12:08 pm Post subject: Re: Problems with ....hmm, linker perhaps |
|
|
Hi JKop,
| Quote: | If I change
ptr foo_ptr = foo< A;
to
ptr foo_ptr ;
foo_ptr = foo< A;
it compiles and links fine as well with MSVC 7.1.
Well, without scrutanizing your code too much, the only difference in the
above is that the former calls a constructor, while the latter calls the
default constructor and then calls operator=.
|
[answer for JKop]
Bullshit.
[answer for the rest]
I doubt that for function pointers constructors must be called. Is this
compiler dependent? Whats the terminology for built-in types?
Reagrds,
Patrick
|
|
| Back to top |
|
 |
Patrick Kowalzick Guest
|
Posted: Fri Oct 29, 2004 12:11 pm Post subject: Re: Problems with ....hmm, linker perhaps |
|
|
Dear Unforgiven,
| Quote: | IMO the following program is legal and should compile.
I concur with your opinion. I tested and verified that VC7.1 does not. I
think it's not a problem with the linker though, it looks as if the
compiler
isn't instantiating the foo<A template, so when the linker looks
for
it, it finds nothing.
I also tested this under Visual C++ 2005 (VC8) beta 1
([url]http://lab.msdn.microsoft.com/vs2005/)[/url], and it compiles and links fine,
which seems to suggest this is indeed a bug in VC7.1 which has been fixed
for VC8.
|
Thanks a lot.
So it seems that I have to search a workaround for my application , which
has a little bit different structure compared to the testcase.
Patrick
|
|
| Back to top |
|
 |
JKop Guest
|
Posted: Fri Oct 29, 2004 12:19 pm Post subject: Re: Problems with ....hmm, linker perhaps |
|
|
| Quote: | [answer for JKop]
Bullshit.
|
Perhaps I should be more general:
A) AnyClass blah = some_expression;
This calls a contructor, supplying "some_expression" as an argument.
B) AnyClass blah; blah = some_expression;
This calls the default constructor. It then calls operator=.
Use your own intelligence to see what I'm getting at.
-JKop
|
|
| Back to top |
|
 |
Patrick Kowalzick Guest
|
Posted: Fri Oct 29, 2004 12:29 pm Post subject: Re: Problems with ....hmm, linker perhaps |
|
|
| Quote: | [answer for JKop]
Bullshit.
|
Yep.
| Quote: |
Perhaps I should be more general:
A) AnyClass blah = some_expression;
This calls a contructor, supplying "some_expression" as an argument.
B) AnyClass blah; blah = some_expression;
This calls the default constructor. It then calls operator=.
|
You are talking about classes, don't you?
See the OP, I was talking about function pointers.
Regards,
Patrick
|
|
| Back to top |
|
 |
Richard Herring Guest
|
Posted: Fri Oct 29, 2004 12:38 pm Post subject: Re: Problems with ....hmm, linker perhaps |
|
|
In message <5zqgd.40415$Z14.14642 (AT) news (DOT) indigo.ie>, JKop <NULL (AT) NULL (DOT) NULL>
writes
| Quote: |
[answer for JKop]
Bullshit.
Perhaps I should be more general:
A) AnyClass blah = some_expression;
This calls a contructor, supplying "some_expression" as an argument.
B) AnyClass blah; blah = some_expression;
This calls the default constructor. It then calls operator=.
Use your own intelligence to see what I'm getting at.
|
What is the type of foo_ptr?
--
Richard Herring
|
|
| Back to top |
|
 |
Rob Williscroft Guest
|
Posted: Fri Oct 29, 2004 12:56 pm Post subject: Re: Problems with ....hmm, linker perhaps |
|
|
Patrick Kowalzick wrote in news:clt96o$icc$1 (AT) sunnews (DOT) cern.ch in
comp.lang.c++:
| Quote: | int main()
{
typedef B< int >( * ptr )(); // Function pointer
ptr foo_ptr = foo< A; // Initialization of function pointer
return 0;
}
//***** code end *********
It builds fine with g++ and MSVC7.1, but the latter one fails linking:
unresolved external symbol "struct B<int> __cdecl foo<struct A
(void)" (??$foo@U?$A@H@@@@YA?AU?$B@H@@XZ) referenced in function
_main
|
This is a bug in MSVC 7.1, Its "forgoten" to do the implicit
instantiation of foo< A< int > >.
I haven't seen it in precisly this context (taking a function pointer)
but in a few other's I have.
As a minor data point I compiled with:
cl -nologo -Za -Zc:forScope,wchar_t -EHsc -O1 -ML -Tptest.cpp
and msvc *didn't* exhibit the problem, but if I compiled:
cl test.cpp
I got the unresolved external, I don't know which combination of
option's achived this turnaround or why.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
|
|
| Back to top |
|
 |
Andrey Tarasevich Guest
|
Posted: Fri Oct 29, 2004 5:07 pm Post subject: Re: Problems with ....hmm, linker perhaps |
|
|
Patrick Kowalzick wrote:
| Quote: | ...
int main()
{
typedef B< int >( * ptr )(); // Function pointer
ptr foo_ptr = foo< A; // Initialization of function pointer
return 0;
}
//***** code end *********
It builds fine with g++ and MSVC7.1, but the latter one fails linking:
unresolved external symbol "struct B<int> __cdecl foo<struct A(void)"
(??$foo@U?$A@H@@@@YA?AU?$B@H@@XZ) referenced in function _main
...
|
Seems to be a compiler problem. Try using the address-of operator explicitly
ptr foo_ptr = &foo< A;
I remember that sometimes it helped to solve similar problems in MSVC++
6, but I don't know whether it will help in MSVC++ 7.
Explicit instantiation will probably also solve the problem, but that
would be a high-maintenance solution.
--
Best regards,
Andrey Tarasevich
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Fri Oct 29, 2004 7:28 pm Post subject: Re: Problems with ....hmm, linker perhaps |
|
|
There is a known bug in visual studio 6 when defining
function templates that don't have the templated variables
in their signature.
|
|
| Back to top |
|
 |
Patrick Kowalzick Guest
|
Posted: Mon Nov 01, 2004 8:45 am Post subject: Re: Problems with ....hmm, linker perhaps |
|
|
Hi Andrey,
| Quote: | Seems to be a compiler problem. Try using the address-of operator
explicitly
ptr foo_ptr = &foo< A;
|
same problem,...
| Quote: | I remember that sometimes it helped to solve similar problems in MSVC++
6, but I don't know whether it will help in MSVC++ 7.
Explicit instantiation will probably also solve the problem, but that
would be a high-maintenance solution.
|
but while I get around it like this:
ptr foo_ptr;
foo_ptr = &foo< A;
it is ok for now . I documented in my code as a bug. Shall be enough....
Thanks,
Patrick
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|