Pages: « 1 2 3 4 5 6 »
  Print  
Author Topic: Happy New Year  (Read 10829 times)
Offline (Unknown gender) luiscubal
Reply #60 Posted on: January 26, 2010, 09:29:21 AM
Member
Joined: Jun 2009
Posts: 452

View Profile Email
It was just an example. There might be better use cases.
Also, how do you declare an array size in a constructor without using operator new nor malloc/calloc?
Logged
Offline (Male) Rusky
Reply #61 Posted on: January 26, 2010, 12:08:20 PM

Resident Troll
Joined: Feb 2008
Posts: 955
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
The C++ implementation of templates generates a new copy of the code every time, which means that non-type parameters are especially non-useful. If runtime generics had non-type parameters, it might be more useful, but that's basically just moving the argument and it may or may not make anything clearer or easier.

Also:
Code: [Select]
class A {
    int array[10];
};
Logged
Offline (Male) RetroX
Reply #62 Posted on: January 26, 2010, 02:56:25 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Well, you could have this

Code: [Select]
int sum(Array<int>* numbers){
   int result = 0;
   for(int i = 0; i < numbers->getLength();++i){
       result += numbers[i];
   }
   return result;
}

int main(){
    StaticArray<int, 10> array1;
    StaticArray<int, 20> array2;
    printf("%d\n", sum(&array1) + sum(&array2));
    return 0;
}
Also this:
Code: [Select]
template<class T,int size> int sum(array<T,size> numbers)
 {
 ...
 }

EDIT: Rusky, it's not really any more or less efficient.  If anything, a template uses less space.  And, it's usually a bit cleaner, sometimes.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Rusky
Reply #63 Posted on: January 26, 2010, 03:25:29 PM

Resident Troll
Joined: Feb 2008
Posts: 955
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
No, they use more space. Every instantiation generates another copy of the code. That's multiple functions doing exactly the same thing, or close enough that you could just change a number at runtime to get the same effect.
Logged
Offline (Unknown gender) luiscubal
Reply #64 Posted on: January 27, 2010, 10:46:55 AM
Member
Joined: Jun 2009
Posts: 452

View Profile Email
Unfortunately, C/C++ arrays have no "length" property, unlike Java arrays. Hence the purpose of my code.
Of course, I could think of ways to optimize my Array/StaticArray classes.
Logged
Offline (Male) RetroX
Reply #65 Posted on: January 27, 2010, 08:35:46 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Unfortunately, C/C++ arrays have no "length" property, unlike Java arrays. Hence the purpose of my code.
Of course, I could think of ways to optimize my Array/StaticArray classes.
That's because it would require storing it in another variable, using up space.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Unknown gender) luiscubal
Reply #66 Posted on: January 28, 2010, 09:41:13 AM
Member
Joined: Jun 2009
Posts: 452

View Profile Email
@RetroX I know C can be used in very limited environments and is quite old.
But nowadays, are 4 bytes per array really that harmful?
Logged
Offline (Male) Rusky
Reply #67 Posted on: January 28, 2010, 12:24:57 PM

Resident Troll
Joined: Feb 2008
Posts: 955
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
It's not that it's harmful, it's that it's not under your control. The whole idea of C and C++ is that you control everything. Make your own array with a length property if you need it, or use vector or something.
Logged
Offline (Unknown gender) score_under
Reply #68 Posted on: January 28, 2010, 03:52:02 PM

Member
Joined: Aug 2008
Posts: 308

View Profile
It's not that it's harmful, it's that it's not under your control. The whole idea of C and C++ is that you control everything. Make your own array with a length property if you need it, or use vector or something.
For once, I agree with what Rusky says.
Logged
Offline (Unknown gender) luiscubal
Reply #69 Posted on: January 28, 2010, 04:50:46 PM
Member
Joined: Jun 2009
Posts: 452

View Profile Email
Quote
Make your own array
In case you didn't notice, that's what I did in the template example above.
Logged
Offline (Male) Rusky
Reply #70 Posted on: January 28, 2010, 06:01:29 PM

Resident Troll
Joined: Feb 2008
Posts: 955
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
Exactly. You just did it with a non-class template parameter, which generates a new copy of all member functions for every size. Why not make it a property and pass it to the constructor?
Logged
Offline (Male) RetroX
Reply #71 Posted on: January 28, 2010, 06:28:18 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Exactly. You just did it with a non-class template parameter, which generates a new copy of all member functions for every size. Why not make it a property and pass it to the constructor?
AFAIK, I thought that the point of templates is so that you don't waste size copying all of the functions.  Meaning that it uses the template to make a function on runtime, or something, unless I'm wrong.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Josh @ Dreamland
Reply #72 Posted on: January 28, 2010, 08:02:06 PM

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2949

View Profile Email
Quite the opposite. Templates (in theory) save lines of code, but increase actual executable size (disk and memory) due to the fact that the code is copied with only slight modification for each instantiation. A perk is that templated functions that go unused aren't included in the compiled product at all.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Unknown gender) Micah
Reply #73 Posted on: January 28, 2010, 11:09:15 PM

Resident Troll
Joined: May 2008
Posts: 129

View Profile
Templates are basically automated copy-and-pasting. They're in C++ because runtime generics would be completely against the philosophy of the language.
Logged
Offline (Unknown gender) luiscubal
Reply #74 Posted on: January 29, 2010, 02:54:47 PM
Member
Joined: Jun 2009
Posts: 452

View Profile Email
While it is true that templates can fill space in a horrible way, they can be very useful.
As miky pointed out, they are automated copy and pasting. So if you actually need copy and pasting, then templates are probably the best solution.

Templates are a way of doing metaprogramming in C++. Not sure how useful that is, though.

Also, while it is true that templates copy-paste the code, that fact means the compiler can attempt more sophisticated tricks to improve runtime speed. So templates can improve speed at the expense of executable size.
Logged
Pages: « 1 2 3 4 5 6 »
  Print