I am confused.
Why does this not work?
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
template<class Tvoid PrintArray(T data[], int start, int stop, int
col_length = 3) {
for (int i = start; i <= stop; i++)
std::cout << std::setw(col_length) << data[i] << " ";
std::cout << std::endl;
}
template<class Tvoid PrintArray(T data[], int col_length = 3) {
//sizeof(data)/sizeof(typeof(data[0]) = 0 !:O
PrintArray(data, 0, sizeof(data)/sizeof(typeof(data[0])), col_length);
}
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
---- ----
If i run the same expression in my main, i get the correct result.. :( ????
--
With regards,
Nick 5 1737
Nick Valeontis wrote:
I am confused.
Why does this not work?
Because an array in an argument list really is a pointer. Try:
void foo(int data[]) {
std::cout << sizeof(data)/sizeof(data[0]);
}
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
template<class Tvoid PrintArray(T data[], int start, int stop, int
col_length = 3) {
for (int i = start; i <= stop; i++)
std::cout << std::setw(col_length) << data[i] << " ";
std::cout << std::endl;
}
template<class Tvoid PrintArray(T data[], int col_length = 3) {
//sizeof(data)/sizeof(typeof(data[0]) = 0 !:O
PrintArray(data, 0, sizeof(data)/sizeof(typeof(data[0])), col_length);
}
try:
template <class T, std::size_t N>
void PrintArray(T data[N], int col_length = 3) {
PrintArray(data, 0, N, col_length);
}
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
----
---- ----
If i run the same expression in my main, i get the correct result.. :(
Yes. As you can see, sizeof is a compile time operator, that is the result
is known at compile time. If your function had worked, this would have
required one instance of PrintArray(T[],int) for each size of the array.
????
btw, typeof isn't part of standard C++.
--
rbh
"Nick Valeontis" <nu*****@freemail.grwrote in message
news:f7*********@mouse.otenet.gr...
:I am confused.
:
: Why does this not work?
:
: ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
----
: template<class Tvoid PrintArray(T data[], int start, int stop, int
: col_length = 3) {
: for (int i = start; i <= stop; i++)
: std::cout << std::setw(col_length) << data[i] << " ";
: std::cout << std::endl;
: }
:
: template<class Tvoid PrintArray(T data[], int col_length = 3) {
In this context, T data[] declares a parameter of
type pointer-to-T.
: //sizeof(data)/sizeof(typeof(data[0]) = 0 !:O
: PrintArray(data, 0, sizeof(data)/sizeof(typeof(data[0])),
col_length);
: }
What you want to do instead is declare the parameter
as a reference to an array:
template<class T, unsigned Ninline
void PrintArray(T (&data)[N], int col_length = 3)
{ PrintArray(data,0,N,col_length); }
This should do what you expected...
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ < http://www.brainbench.com
K, thanks both.
I think i have understood why this is happening.
Now, i tested your suggestions. This works okay and does what I want indeed:
template<class T, unsigned Nvoid PrintArray(T (&data)[N], int col_length =
3);
However, this does not work:
template<class T, unsigned Nvoid PrintArray(T data[N], int col_length =
3);
I get "no matching function for call to `PrintArray(int[10])' " error when i
try something like this:
--- --- --- --- --- ---
int data[] = { 9,7,5,8,2,6,4,3,1,0 };
PrintArray(data);
--- --- --- --- --- ---
Why is that? :/
--
With regards,
Nick
Nick Valeontis wrote:
K, thanks both.
I think i have understood why this is happening.
Now, i tested your suggestions. This works okay and does what I want
indeed:
template<class T, unsigned Nvoid PrintArray(T (&data)[N], int col_length
= 3);
However, this does not work:
template<class T, unsigned Nvoid PrintArray(T data[N], int col_length =
3);
I get "no matching function for call to `PrintArray(int[10])' " error when
i try something like this:
--- --- --- --- --- ---
int data[] = { 9,7,5,8,2,6,4,3,1,0 };
PrintArray(data);
--- --- --- --- --- ---
Why is that? :/
I was a little too sloppy while posting. The leftmost array dimension is not
really part of the type when the array is a parameter, because an array is
a pointer to its first element when it is a function parameter. There's
even a note in the standard specifying that the second example shouldn't
work. Making data a reference changes this.
--
rbh
ok,
I 'd like to thank you all who answered!
You have been very informative ;-)
-Nick This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: bjam |
last post by:
Help! The apply-templates function is not currently allowing me to
select a specific template... eventhough I tried putting a select
statement, it does not seem to work??? Can someone help show...
|
by: Canonical Latin |
last post by:
"Leor Zolman" <leor@bdsoft.com> wrote
> "Canonical Latin" <javaplus@hotmail.com> wrote:
>
> > ...
> >But I'm still curious as to the rational of having type
>...
|
by: Jürgen Kaminski |
last post by:
Hello all,
I got some legacy code where a template implements a non-templated
interface. These classes are really HUGE, but it boils down to the
following structure:
class BaseClass
{
public:...
|
by: Bo Xu |
last post by:
Object of Combination
By Bo Xu
Introduction
A combination of n things, taken s at a time, often referred as an
s-combination out of n, is a way to select a subset of size s from a
given set of...
|
by: Linny |
last post by:
Hi,
I need some help in declaring an array of pointers to array of a
certain fixed size. I want the pointers to point to arrays of fixed
size only (should not work for variable sized arrays of the...
|
by: Andreas Micheler |
last post by:
Hi,
I have several long complex C macros in the math module of aUCBLogo like
#define _XFUNC_primitive(NAME)\
NodeP _##NAME(PairPP arg)\
{ ((ObjectP)(arg))->var()->NAME( arg );\
return...
|
by: Martin Jørgensen |
last post by:
Hi,
I'm relatively new with C-programming and even though I've read about
pointers and arrays many times, it's a topic that is a little confusing
to me - at least at this moment:
----
1)...
|
by: sarathy |
last post by:
Hi all,
I need a small clarification reg. Templates and
Polymorphism. I believe templates is really a good feature, which can
be used to implement generic functions and classes. But i doubt...
|
by: John Goche |
last post by:
Hello,
I was wondering what the pros and cons are for using
numbers in templates rather than in constructors as in:
MyBuffer<10foo;
rather than
MyBuffer foo(10);
|
by: Zara |
last post by:
I have been trying to substitute the macro that comes with the
compiler with the following template:
template <typename ITEM,size_t SIZE>
inline size_t arraySize(ITEM const (&array)) {
return...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| | |