Including a (renamed) source file is ugly, right?

E

Eric Lilja

Hello, I recently saw code like this:

$ cat t.h
namespace nc{

template<typename T>
class Base {
T hello;

protected:
int test1;
};

template<typename T>
class Next: public Base<T>{

int test();
};
};

#include "t.tmpl"
$ cat t.tmpltemplate<typename T>
int nc::Next<T>::test(){
return test1;
}
I couldn't even get the code to compile until I'd changed return test1;
toreturn Base<T>test1;"t.tmpl" looks like a renamed source (.cpp) file and
it's used to work around the fact that the compilerlacks the export keyword
but the author still wantsto hide the implementation details. I
immediatelythought this was ugly indeed, was I right? If so, why?/ Eric
 
S

Sumit Rajan

Eric Lilja said:
Hello, I recently saw code like this:


$ cat t.h
namespace nc{

template<typename T>
class Base {
T hello;

protected:
int test1;
};

template<typename T>
class Next: public Base<T>{

You really want the following to be a private member?
int test();
};
};

No semi-colon required above.
#include "t.tmpl"
$ cat t.tmpltemplate<typename T>
int nc::Next<T>::test(){
return test1;

Change the above to:

return this->test1;

or

return Base<T>::test1;

For more details on why you need to do this, see:
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12
}

I couldn't even get the code to compile until I'd changed return test1;
toreturn Base<T>test1;"t.tmpl" looks like a renamed source (.cpp) file and

it's used to work around the fact that the compilerlacks the export
keyword but the author still wantsto hide the implementation details. I
immediatelythought this was ugly indeed, was I right? If so, why?/ Eric

Personally, I find it ugly too. But the lack of an export keyword in most
compilers is the reason why people have to do this (or something similar to
this).

You may also find Q7 to Q9 helpful:
http://www.parashift.com/c++-faq-lite/templates.html

Regards,
Sumit.
 
S

Sumit Rajan

Sumit Rajan said:
But the lack of an export keyword in most compilers

Let me try to rephrase that:

But the fact that most compilers have not (yet) implemented support for the
export keyword ...

Regards,
Sumit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top