 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
cg Guest
|
Posted: Tue Oct 26, 2004 10:50 pm Post subject: Template class with no arguments |
|
|
/*
Hi,
This file is compilable for your coding pleasure. :)
I have sort of a strange question. I am using class templates to
guarantee that a set of functionality will be compiled inline rather than
be evaluate to a function call, for performance.
First of all, is this a valid/justifiable use of class templates, and
secondly, can I declare a template class with no argument list?
For background -- I'm essentially creating a 2-dimensional array which
can be accessed with a negative index (or a positive index which would
normally be "out of bounds") by creating a buffer zone around the
"interesting" portion of the array. To give an example (best viewed with
fixed font):
0 0 0 0 0
0 1 1 1 0
0 1 1 1 0
0 1 1 1 0
0 0 0 0 0
In the diagram above, I would call the middle portion of the array
containing the values of 1 "interesting", and the parts containing zeros
"uninteresting".
The template classes I've created allow me to access the interesting
portion of the diagram above starting at index (0,0), whereas the
uninteresting portion starts at (-1,-1).
Here is my code, which hopefully will make it more clear: (by the way --
I do realize that the following code is not highly optimized, so don't
feel like you need to point that out)
*/
template <int W, int B> class BoardRow
{
private:
char m_row[W+B*2];
public:
char &operator[](int x)
{
return m_row[x+B];
}
};
template <int WIDTH, int HEIGHT, int BUFFER> class BoardRows
{
private:
BoardRow<WIDTH, BUFFER> m_rows[HEIGHT+BUFFER*2];
public:
BoardRow<WIDTH, BUFFER> &operator[](int y)
{
return m_rows[y+BUFFER];
}
};
template <int I> class CheckerboardData
{
private:
BoardRows<8,8,2> m_rows;
public:
char getNotationSquare(int sqNum)
{
int sqRow = (sqNum-1) / 4;
int sqCol = (sqNum - 1 - sqRow * 4) * 2 + (sqRow+1) % 2;
return m_rows[sqRow][sqCol];
}
void setNotationSquare(int sqNum, char sqType)
{
int sqRow = (sqNum-1) / 4;
int sqCol = (sqNum - 1 - sqRow * 4) * 2 + (sqRow+1) % 2;
m_rows[sqRow][sqCol] = sqType;
}
BoardRow<8,2> &operator[](int y)
{
return m_rows[y];
}
};
typedef CheckerboardData<0> CheckerBoardDataType;
/*
Note that my CheckerboardData template class requires a parameter <I>
which is never even used inside of the template. I put that parameter in
to allow the class to compile, and for no other reason. Since I'm writing
this code for pleasure, I'm very picky about it and the unneeded template
argument bothers me.
What should I do instead?
*/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Oct 27, 2004 2:06 pm Post subject: Re: Template class with no arguments |
|
|
cg <nunya (AT) damspam (DOT) com> wrote
| Quote: | I have sort of a strange question. I am using class templates to
guarantee that a set of functionality will be compiled inline rather
than be evaluate to a function call, for performance.
|
There is absolutely no way you can ensure that something is generated
inline or not, within the language. Whether a function is generated
inline or not is not part of the observable behavior, and has no effect
on the observable behavior, in the sense of the standard. (Obviously,
generating a function inline may make the program run faster. Or
slower, for that matter. But the standard does not consider program
speed as part of the observable behavior.)
| Quote: | First of all, is this a valid/justifiable use of class templates, and
secondly, can I declare a template class with no argument list?
|
First of all, I don't see how using a template changes anything in the
problem, and secondly, no.
[...]
| Quote: | template <int I> class CheckerboardData
{
private:
BoardRows<8,8,2> m_rows;
public:
char getNotationSquare(int sqNum)
{
int sqRow = (sqNum-1) / 4;
int sqCol = (sqNum - 1 - sqRow * 4) * 2 + (sqRow+1) % 2;
return m_rows[sqRow][sqCol];
}
void setNotationSquare(int sqNum, char sqType)
{
int sqRow = (sqNum-1) / 4;
int sqCol = (sqNum - 1 - sqRow * 4) * 2 + (sqRow+1) % 2;
m_rows[sqRow][sqCol] = sqType;
}
BoardRow<8,2> &operator[](int y)
{
return m_rows[y];
}
};
typedef CheckerboardData<0> CheckerBoardDataType;
Note that my CheckerboardData template class requires a parameter
which is never even used inside of the template.
|
So why is the class a template?
| Quote: | I put that parameter in to allow the class to compile, and for no
other reason. Since I'm writing this code for pleasure, I'm very picky
about it and the unneeded template argument bothers me.
What should I do instead?
|
Don't use a template unless you need one.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Wed Oct 27, 2004 2:08 pm Post subject: Re: Template class with no arguments |
|
|
cg wrote:
| Quote: | I have sort of a strange question. I am using class templates to
guarantee that a set of functionality will be compiled inline rather than
be evaluate to a function call, for performance.
|
There is no such guarantee. It is just as with the keyword 'inline', which
is a mere suggestion.
In practice, 'inline' functions are created as normal functions that can be
called, with all the overhead of pushing arguments on the stack etc. The
only difference is that they are declared 'weak' to the linker so it simply
discards additional instances thereof. Additionally, they might be inlined,
but that can also happen to functions that were not declared with 'inline'.
| Quote: | First of all, is this a valid/justifiable use of class templates, and
secondly, can I declare a template class with no argument list?
|
valid: yes
justifyable: probably not
secondly: no, but nothing forces you to use it
Uli
--
FAQ: http://parashift.com/c++-faq-lite/
/* bittersweet C++ */
default: break;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
cg Guest
|
Posted: Fri Oct 29, 2004 1:42 am Post subject: Re: Template class with no arguments |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote in
news:d6652001.0410270029.7e43eac0 (AT) posting (DOT) google.com:
| Quote: | cg <nunya (AT) damspam (DOT) com> wrote in message
news:<Xns958E17FDE729Akenwoodfanrrcom (AT) 24 (DOT) 93.44.119>...
I have sort of a strange question. I am using class templates to
guarantee that a set of functionality will be compiled inline rather
than be evaluate to a function call, for performance.
There is absolutely no way you can ensure that something is generated
inline or not, within the language. Whether a function is generated
inline or not is not part of the observable behavior, and has no
effect on the observable behavior, in the sense of the standard.
(Obviously, generating a function inline may make the program run
faster. Or slower, for that matter. But the standard does not
consider program speed as part of the observable behavior.)
|
Well I suppose I (and many other c++ writers I know) have been laboring
under a false assumption. It has always been my understanding that class
templates were a replacement/improvement for macros, and that they do not
generate native code, but instead are translated into c++ code inline, and
then compiled. Certainly a macro never generates a function? If a template
class can actually generate a function, perhaps I should use macros
instead.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Fri Oct 29, 2004 2:35 pm Post subject: Re: Template class with no arguments |
|
|
cg <nunya (AT) damspam (DOT) com> wrote
| Quote: | kanze (AT) gabi-soft (DOT) fr wrote in
news:d6652001.0410270029.7e43eac0 (AT) posting (DOT) google.com:
cg <nunya (AT) damspam (DOT) com> wrote in message
news:<Xns958E17FDE729Akenwoodfanrrcom (AT) 24 (DOT) 93.44.119>...
I have sort of a strange question. I am using class templates to
guarantee that a set of functionality will be compiled inline
rather than be evaluate to a function call, for performance.
There is absolutely no way you can ensure that something is
generated inline or not, within the language. Whether a function is
generated inline or not is not part of the observable behavior, and
has no effect on the observable behavior, in the sense of the
standard. (Obviously, generating a function inline may make the
program run faster. Or slower, for that matter. But the standard
does not consider program speed as part of the observable behavior.)
Well I suppose I (and many other c++ writers I know) have been
laboring under a false assumption. It has always been my understanding
that class templates were a replacement/improvement for macros, and
that they do not generate native code, but instead are translated into
c++ code inline, and then compiled. Certainly a macro never generates
a function? If a template class can actually generate a function,
perhaps I should use macros instead.
|
Where on earth did you get this idea?
Historically, many early template implementations did work sort of like
macros, in the sense that the generated code was more or less as if one
had replaced the template parameters with the template arguments, and
compiled. But even then, the functions were only inline if they would
have been inline if the code had been written without templates.
And templates (class and otherwise) are in some ways a
replacement/improvement for macros. At least, they are a replacement
for the macros in <generic.h>. But the macros in <generic.h> didn't
generate functions inline either, unless you requested the function to
be inline.
When we speak of "inline" in C++, there are actually two different
things that we can mean: what we ask the compiler to do, and what it
does. Because the keyword "inline" is only a recommendation -- the
compiler is not required to generate an inline function inline, and it
is not forbidden from generating other functions inline. If we speak of
intent, or quality of implementation, of course, the difference is less
significant, although a really good compiler may still inline some
functions without my asking for it.
The request for inline is orthogonal with templates. Any function may
be declared inline, and any function defined in the body of a class is
implicitly declared inline.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
cg Guest
|
Posted: Sun Oct 31, 2004 11:18 am Post subject: Re: Template class with no arguments |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote in
news:d6652001.0410290155.47141e0e (AT) posting (DOT) google.com:
| Quote: | Where on earth did you get this idea?
|
I'm not exactly sure where I got the idea. Thanks for the information
though.
Just FYI, my compiler refused to inline the following function (I looked at
the assembly code).
class Board {
public:
char m_squares[12][12];
inline char * operator[(int index)
{
return m_board[index+2] + 2;
}
}
So it looks as though I have to use the EVIL construct, the macro function,
if I am to ensure that the compilation is inline.
Something like:
#define MB(x, y) m_board.m_squares[y+2][x+2]
I don't like using evil constructs. Maybe someone can talk me out of it.:)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Tue Nov 02, 2004 1:17 pm Post subject: Re: Template class with no arguments |
|
|
cg <nunya (AT) damspam (DOT) com> wrote
| Quote: | kanze (AT) gabi-soft (DOT) fr wrote in
news:d6652001.0410290155.47141e0e (AT) posting (DOT) google.com:
Where on earth did you get this idea?
I'm not exactly sure where I got the idea. Thanks for the information
though.
Just FYI, my compiler refused to inline the following function (I
looked at the assembly code).
class Board {
public:
char m_squares[12][12];
inline char * operator[(int index)
{
return m_board[index+2] + 2;
}
}
|
With what options? I've never seen a compiler which wouldn't inline
this when inlining was authorized. (I'm supposing that m_board is a
typo for m_squares above. Otherwise, of course, if it is something else
with a complicated overloaded [] which returns a proxy, etc., you might
find some compilers which have problems.)
Note that with most compilers, inlining is turned of by default, and
with almost all compilers, it is turned off by default if you have
passed options for debugging. If your concern is performance, make sure
your using /Ox /Ob2 with Microsoft, or -O3 with g++, before doing
anything else.
And BTW: you don't need the inline keyword if the function is defined in
the scope of the class. It's the default. (Many of us, however, prefer
NOT to clutter the class with function definitions, and will define the
inline functions outside of the class, but still in the header. In this
case, inline IS necessary.)
| Quote: | So it looks as though I have to use the EVIL construct, the macro
function, if I am to ensure that the compilation is inline.
|
Formally, even a macro doesn't guarantee that the code is inline,
although I don't know of a compiler where it isn't. But then, I don't
know of a compiler where really simple inline functions like the above
aren't inlined if the correct options are given.
| Quote: | Something like:
#define MB(x, y) m_board.m_squares[y+2][x+2]
I don't like using evil constructs. Maybe someone can talk me out of
it.
|
Check your compiler options first.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|