Go Back   Rhinocerus > Newsgroup > Newsgroup comp.language.c++



Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 02-10-2010, 02:02 PM
jburgy
Guest
 
Posts: n/a
Default How to debug method resolution problem?

Good morning,

I'm faced with a tricky problem and the C++ gurus I work with couldn't
help. I apologize for not posting actual code, I simply can't for IP
reasons. I have the following inheritance diagram:

class A
{
virtual A *foo( ... ) const;
};

template< class T >
class B : public A
{
virtual A *foo( ... ) const;
};

template< class T >
inline A *B::foo( ... ) const
{
throw( "B's template specializations must override foo!" );
return NULL;
}

I have specializations of B for 8 distinct classes T. I have
explicitly overridden foo for all of them (using a preprocessor macro)
but am getting mixed results. Some cases still end up throwing the
error message shown above (3 when building with MSVC and 5 with g++
although I have no compile errors in either case). Is this something
that's not supported by the standard and I shouldn't expect it to work
reliably? How do I go about debugging it since the problem occurs at
compile-time.

Thanks,
Reply With Quote
Alt Today
Advertising
Google Adsense
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 02-10-2010, 02:58 PM
Michael Doubez
Guest
 
Posts: n/a
Default Re: How to debug method resolution problem?

On 10 fév, 15:02, jburgy <jbu...@gmail.com> wrote:
> Good morning,
>
> I'm faced with a tricky problem and the C++ gurus I work with couldn't
> help. *I apologize for not posting actual code, I simply can't for IP
> reasons. *I have the following inheritance diagram:
>
> class A
> {
> * * virtual A *foo( ... ) const;
>
> };
>
> template< class T >
> class B : public A
> {
> * * virtual A *foo( ... ) const;
>
> };
>
> template< class T >
> inline A *B::foo( ... ) const
> {
> * * throw( "B's template specializations must override foo!" );
> * * return NULL;
>
> }
>
> I have specializations of B for 8 distinct classes T. *I have
> explicitly overridden foo for all of them (using a preprocessor macro)
> but am getting mixed results. *Some cases still end up throwing the
> error message shown above (3 when building with MSVC and 5 with g++
> although I have no compile errors in either case). *Is this something
> that's not supported by the standard and I shouldn't expect it to work
> reliably?


Yes. It is a valid specialisation. But definition should use B<T> :
template< class T >
A *B<T>::foo( ... ) const
{
throw( "B's template specializations must override foo!" );
return NULL;
}

> *How do I go about debugging it since the problem occurs at
> compile-time.


Without the exact message, I can't say.
But instead of using a run time check you could simply make it pure
virtual:

template< class T >
class B : public A
{
virtual A *foo( ... )const = 0;
};

Then, any class inheriting from B<T> must implement foo to be
instantiatable.
Note: from §10.4/5 of the standard: [...] a pure virtual function may
override a virtual function which is not pure.

--
Michael
Reply With Quote
  #3 (permalink)  
Old 02-10-2010, 03:06 PM
Yu Han
Guest
 
Posts: n/a
Default Re: How to debug method resolution problem?

May be you could declare A::foo as pure virtual. But I guess you do have
something useful in A::foo, so you have to do something different.
Following is a solution that partially solve your problem:

On 02/10/2010 10:02 PM, jburgy wrote:
> Good morning,
>
> I'm faced with a tricky problem and the C++ gurus I work with couldn't
> help. I apologize for not posting actual code, I simply can't for IP
> reasons. I have the following inheritance diagram:
>
> class A
> {
> virtual A *foo( ... ) const;
> };
>
> template< class T>
> class B : public A
> {
> virtual A *foo( ... ) const;

private:
// add a pure virtual function
virtual A* bar( ... ) const = 0;
> };


then compiler would give you a error for auto instantiation of B.
But for specializations by yourself you must to give a definition for
bar and, just call it from foo.

The problem is that if you do not provide a foo definition for B's
template specialization, you still get no errors at compile time. Since
it would get one from A.

>
> template< class T>
> inline A *B::foo( ... ) const
> {
> throw( "B's template specializations must override foo!" );
> return NULL;
> }
>
> I have specializations of B for 8 distinct classes T. I have
> explicitly overridden foo for all of them (using a preprocessor macro)
> but am getting mixed results. Some cases still end up throwing the
> error message shown above (3 when building with MSVC and 5 with g++
> although I have no compile errors in either case). Is this something
> that's not supported by the standard and I shouldn't expect it to work
> reliably? How do I go about debugging it since the problem occurs at
> compile-time.
>
> Thanks,



--
Yu Han

--- news://freenews.netfront.net/ - complaints: news@netfront.net ---
Reply With Quote
  #4 (permalink)  
Old 02-10-2010, 03:11 PM
Vladimir Jovic
Guest
 
Posts: n/a
Default Re: How to debug method resolution problem?

jburgy wrote:
> Good morning,
>
> I'm faced with a tricky problem and the C++ gurus I work with couldn't
> help. I apologize for not posting actual code, I simply can't for IP
> reasons. I have the following inheritance diagram:


IP reasons?
You should post minimal compilable example, because nothing is wrong in
the code you posted

>
> class A
> {
> virtual A *foo( ... ) const;
> };
>
> template< class T >
> class B : public A
> {
> virtual A *foo( ... ) const;
> };
>
> template< class T >
> inline A *B::foo( ... ) const


This should be:
inline A *B< T >::foo( ... ) const

> {
> throw( "B's template specializations must override foo!" );
> return NULL;
> }
>

Reply With Quote
  #5 (permalink)  
Old 02-10-2010, 03:19 PM
Pete Becker
Guest
 
Posts: n/a
Default Re: How to debug method resolution problem?

Yu Han wrote:
> May be you could declare A::foo as pure virtual. But I guess you do have
> something useful in A::foo, so you have to do something different.


Not necessarily. Declaring a function pure virtual does not mean that
you can't implement it.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
"The Standard C++ Library Extensions: a Tutorial and Reference"
(www.petebecker.com/tr1book)
Reply With Quote
  #6 (permalink)  
Old 02-10-2010, 03:54 PM
Yu Han
Guest
 
Posts: n/a
Default Re: How to debug method resolution problem?

On 02/10/2010 11:19 PM, Pete Becker wrote:
> Yu Han wrote:
>> May be you could declare A::foo as pure virtual. But I guess you do
>> have something useful in A::foo, so you have to do something different.

>
> Not necessarily. Declaring a function pure virtual does not mean that
> you can't implement it.
>


thanks for reminding me. But what if you do want some instances of A?
A* A::foo(...) const
{ return new A; }

--
Yu Han
Reply With Quote
  #7 (permalink)  
Old 02-10-2010, 04:03 PM
jburgy
Guest
 
Posts: n/a
Default Re: How to debug method resolution problem?

On Feb 10, 10:19*am, Pete Becker <p...@versatilecoding.com> wrote:
> Yu Han wrote:
> > May be you could declare A::foo as pure virtual. But I guess you do have
> > something useful in A::foo, so you have to do something different.

>
> Not necessarily. Declaring a function pure virtual does not mean that
> you can't implement it.
>
> --
> * *Pete
> Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
> "The Standard C++ Library Extensions: a Tutorial and Reference"
> (www.petebecker.com/tr1book)


Thanks for all the help. Let me try to address your points:

* I can't add a pure virtual B<T>::foo, too many classes derive from
it and rely on the default implementation
* Once again, sorry for not posting a minimal compilable sample, that
would take very long to reproduce the relevant setup (3 separate
projects compiled independently)
* There is no error message, everything build and runs, I'm just
getting the wrong implementation

I have made accidental progress by remove the inline keyword and now
get a fatal error LNK1169: one or more multiply defined symbols
founds. Those multiply defined symbols are precisely the
specializations that aren't working. Inline somehow made the compiler
feel it was ok to skip them I guess.
Reply With Quote
 
Reply

Popular Tags in the Forum
debug, method, problem, resolution

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem step-in debug through instance_eval Alexandre Mutel Newsgroup comp.lang.ruby 2 11-21-2009 10:47 PM
Seeking computer-programming job (Sunnyvale, CA) Robert Maas Newsgroup comp.lang.lisp 1631 07-14-2009 05:04 AM
Seeking computer-programming job (Sunnyvale, CA) Robert Maas Newsgroup comp.lang.java.programmer 1430 07-13-2009 10:16 PM
Re: Problem with Macro resolution Don Henderson Newsgroup comp.soft-sys.sas 0 04-10-2008 02:41 AM
Re: Problem with Macro resolution Chang Chung Newsgroup comp.soft-sys.sas 0 04-10-2008 12:24 AM



Language 1 | C | C++ | Php | Python | Lisp | Perl | Ruby | Java | Pascal | Basic | Language 2 | Databases | Oracle | Mysql | Access | Drupal
All times are GMT. The time now is 02:00 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.