|
-
May 8th, 2006, 04:06 AM
#1
Blitz Library Strange Initialization
I have been trying to figure out how the Blitz numeric library was able to get commas to initialize Vectors and Matrices. For example,
Code:
TinyMatrix<double, 2, 2> m;
m = 1, 2, 3, 4;
is perfectly valid. I have looked through the source, but I don't see any operator,(T_numtype n) in there. What did he do?
-
May 8th, 2006, 05:39 AM
#2
Re: Blitz Library Strange Initialization
I figured it out! What it does is in the operator= overload for TinyMatrix it returns an object that has operator,(T_numtype) overloaded. That object takes as an argument an iterator to TinyMatrix. The operator, simply increments the iterator each time a new number is encountered, pretty sneaky. I made an ultra simple example utilizing the same trick (without iterators) if anyone here is curious.
Code:
class ListInit
{
int* parr;
public:
ListInit() { parr = NULL; }
ListInit(int* parrz) { parr = parrz; }
ListInit& operator,(int num) {
*parr = num;
parr++;
return *this;
}
};
class Array
{
int arr[3];
public:
Array()
{
arr[0] = 0;
arr[1] = 0;
arr[2] = 0;
}
Array(int x1, int x2, int x3)
{
arr[0] = x1;
arr[1] = x2;
arr[2] = x3;
}
ListInit operator=(int x)
{
arr[0] = x;
return ListInit(&arr[1]);
}
void Print()
{
cout << arr[0] << ", " << arr[1] << ", " << arr[2] << endl;
}
};
int main(int argc, char *argv[])
{
Array x;
x = 1, 2, 3;
x.Print();
return EXIT_SUCCESS;
}
Boost has a library completely dedicated to this kind of assignment. It is called "assign." It seems pretty interesting. Thanks for at least looking.
-
May 8th, 2006, 07:21 AM
#3
Re: Blitz Library Strange Initialization
thats interesting... wuld be good for sizing multidimensionals.
but there is this design issue here what would be best to use?
maybe Array.Size = 1, 2, 3; or Array.Size = [1][2][3]; (if it wuld be possible) or Array.Size[1][2][3] or ()'s
-
May 8th, 2006, 09:11 AM
#4
Re: Blitz Library Strange Initialization
Something like Array.SetSize[1][2][3] should be possible.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
May 8th, 2006, 09:27 AM
#5
Re: Blitz Library Strange Initialization
yes but at design what would be better? Array.Size = 1, 2, 3; or Array.Size[1][2][3];
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|