Re: Non virtual base interface?
"Patrik Kahari" <patrik.kahari@googlemail.com> wrote in
news:1172669833.428062.111680@a75g2000cwd.googlegroups.com:
Hello,
I'd like to separate my public interface declaration from my private.
I want to do this in order to document the public interface to clients
in an uncluttered header file.
This also depends on what you mean by uncluttered. Do you need a
compiler firewall, i.e., you don't want your implementation details at
all visible to class users? Or can you live with inline definitions
in a separate file physically visible to users, but implementation-
only for all intents?
I can do this with ABI. But doing so
means my interface becomes virtual. I'd rather not pay the price of
virtualness when my interface is not meant to be used polymorfically.
I'm unsure if a compiler could optimize away the v-table if no ABI
pointers or references are used. Could it?
[snip]
The compiler can inline virtual funcion calls if it can tell
statically which function to call. Consider the following:
struct Base
{
virtual void doit() = 0;
};
struct Derived: public Base
{
virtual void doit() { cout << "it!" << endl; }
}
// in a different translation unit
void bar(Base & base)
{
base.doit();
}
void foo()
{
Derived derived;
Base & base = derived;
derived.doit(); // obvious--optimized
base.doit(); // ? probably optimized
bar(base); // optimized with link-time optimization
}
The Derived::doit() could be optimized. The direct Base::doit() might
be. The last Base::doit() is unlikely to be optimized.
The vtable will be defined. There's only minor space overhead (a
pointer aligned for your architecture). The time overhead can be
optimized within a translation unit. Outside the translation unit,
you're looking at still cutting-edge compilers.
.....
To get back to the aforementioned CRTP, you could get the compiler
firewall in the implementation class:
class MyImp: public CRTPInterface<MyImp>
{
private:
friend class CRTPInterface<MyImp>;
void doit();
};
But that compiler firewall will make it harder for inline optimization
(unless your compiler does link-time optimization).
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]