|
|||
|
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, |
|
|
||||
|
||||
|
|
|
|||
|
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 |
|
|||
|
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 --- |
|
|||
|
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; > } > |
|
|||
|
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) |
|
|||
|
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 |
|
|||
|
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. |
|
|
|
|
![]() |
| Popular Tags in the Forum |
| debug, method, problem, resolution |
| Thread Tools | |
| Display Modes | |
|
|
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 |