Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   How to debug method resolution problem? (http://www.velocityreviews.com/forums/t714532-how-to-debug-method-resolution-problem.html)

jburgy 02-10-2010 02:02 PM

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,

Michael Doubez 02-10-2010 02:58 PM

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

Vladimir Jovic 02-10-2010 03:11 PM

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;
> }
>


jburgy 02-10-2010 04:03 PM

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.


All times are GMT. The time now is 10:33 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.