W
wogston
A)
template <typename scalar, int size>
struct basevector
{
enum { size = size };
scalar v[size];
};
B)
template <typename scalar, int size>
struct vector : basevector<scalar,size>
{
// ... impl.
};
template <typename scalar>
struct vector<scalar,3> : basevector<scalar,3>
{
// ... specialization related enhancements to impl.
};
OK, the code snips out of the way, the A) segment above declares/defines
base type, from which various different vector types are inherited (vector,
point, quaternion...). The B) segment declares the specific example of
scalar vector type. Why there is enum for size in the basevector, is, that
it makes the size visible to specialized types, which don't have access (as
far as I know of) to the template size argument.
Question: is it legal to write enum { size = size }; .. as far as I
undertand, the scope for left side of assignment is the enumeration, and
right side is the outer scope, in this case template argument. Is this
defined well enough in the standard, so that I can expect compilers not to
give compilation time diagnostic warnings about unsolved resolution and such
(I know these are implementation defined details, but does standard guaratee
the above code snip should compile (assuming there aren't typoes above, I
didn't quite copy-paste from complete code, tried to isolate the code
relevant to the question only).
This is a moot point in practise, I gave the outer template argument name
"vsize", but I am asking for curiosity (it compiled with few compilers I
tested on with the above variation).
If someone is wondering why I want the size visible, it is for recursive
template meta-program which requires the size of the vector as one of the
arguments, to know the number of recursions required to handle all
components of the vector. I'm using same "sourcecode path" to generate all
the specializations, and that requires the "size" enumeration, so that I
don't have to repeat the code 9 times (at this time for: vector<scalar,2>,
vector<scalar,3>, vector<scalar,4>, same for quaternion and point, total: 9
times same code, so it's handy to have the enumeration visible, no?
I'm assuming that the enum { size = size }; is correct and valid c++, but
somehow it looks amazingly stupid, so I renamed the argument so that the
code wouldn't be so bloody obfuscated to the casual observer. What would you
have done in similiar situation?
p.s. namespace collision are easily avoided, since I use these templates
through typedefs, which also have a scope themselves, so in practise it
turns into:
using coremath::float3;
....
float3 v(x,y,z);
float3 u = v * 1.2f + coremath::dot(a,b) * c; // ... etc.
(so apologies, a lot of text for a very short question, is that stupid or
not, if it's stupid, why?
template <typename scalar, int size>
struct basevector
{
enum { size = size };
scalar v[size];
};
B)
template <typename scalar, int size>
struct vector : basevector<scalar,size>
{
// ... impl.
};
template <typename scalar>
struct vector<scalar,3> : basevector<scalar,3>
{
// ... specialization related enhancements to impl.
};
OK, the code snips out of the way, the A) segment above declares/defines
base type, from which various different vector types are inherited (vector,
point, quaternion...). The B) segment declares the specific example of
scalar vector type. Why there is enum for size in the basevector, is, that
it makes the size visible to specialized types, which don't have access (as
far as I know of) to the template size argument.
Question: is it legal to write enum { size = size }; .. as far as I
undertand, the scope for left side of assignment is the enumeration, and
right side is the outer scope, in this case template argument. Is this
defined well enough in the standard, so that I can expect compilers not to
give compilation time diagnostic warnings about unsolved resolution and such
(I know these are implementation defined details, but does standard guaratee
the above code snip should compile (assuming there aren't typoes above, I
didn't quite copy-paste from complete code, tried to isolate the code
relevant to the question only).
This is a moot point in practise, I gave the outer template argument name
"vsize", but I am asking for curiosity (it compiled with few compilers I
tested on with the above variation).
If someone is wondering why I want the size visible, it is for recursive
template meta-program which requires the size of the vector as one of the
arguments, to know the number of recursions required to handle all
components of the vector. I'm using same "sourcecode path" to generate all
the specializations, and that requires the "size" enumeration, so that I
don't have to repeat the code 9 times (at this time for: vector<scalar,2>,
vector<scalar,3>, vector<scalar,4>, same for quaternion and point, total: 9
times same code, so it's handy to have the enumeration visible, no?
I'm assuming that the enum { size = size }; is correct and valid c++, but
somehow it looks amazingly stupid, so I renamed the argument so that the
code wouldn't be so bloody obfuscated to the casual observer. What would you
have done in similiar situation?
p.s. namespace collision are easily avoided, since I use these templates
through typedefs, which also have a scope themselves, so in practise it
turns into:
using coremath::float3;
....
float3 v(x,y,z);
float3 u = v * 1.2f + coremath::dot(a,b) * c; // ... etc.
(so apologies, a lot of text for a very short question, is that stupid or
not, if it's stupid, why?