B
Brian Byrne
I've recently developed a minor interest in Expression Templates and,
in attempt to further my understanding of template metaprogramming,
have been trying to create my own implementation. It seems I've ran
into a problem that, while there are ways around it (causing way for
code duplication), I'm not too sure if a direct solution exists. Given:
template< typename T >
class A { ... };
template< typename T >
void Foo( const A< T >& a ) { ... }
class B { ... };
class C { ... };
Here, I can call Foo directly just fine:
A< B > ab;
Foo( ab );
But suppose I wanted to be able to pass a built-in type to Foo. For
example, a float, and have it IMPLICITLY (key!) be converted to a type
A< C >. Then I could do the following without problem:
float f = 5.0f;
Foo( f ); // ideally implicitly casted to: Foo( A< C >( 5.0f ) );
To get this implicit cast, I figured I could specialize A; something
along the lines of:
template<>
class A< C > {
...
public: A( float f ) { ... }
};
Now A< C > has a non-explicit constructor taking a float, and if C is
the only class used as a template parameter for A, then I had hoped the
compiler could unambiguously do the cast. Unfortunately, the compiler
looked right over it.
So, is it possible?
Any help appreciated!
-- Brian Byrne
in attempt to further my understanding of template metaprogramming,
have been trying to create my own implementation. It seems I've ran
into a problem that, while there are ways around it (causing way for
code duplication), I'm not too sure if a direct solution exists. Given:
template< typename T >
class A { ... };
template< typename T >
void Foo( const A< T >& a ) { ... }
class B { ... };
class C { ... };
Here, I can call Foo directly just fine:
A< B > ab;
Foo( ab );
But suppose I wanted to be able to pass a built-in type to Foo. For
example, a float, and have it IMPLICITLY (key!) be converted to a type
A< C >. Then I could do the following without problem:
float f = 5.0f;
Foo( f ); // ideally implicitly casted to: Foo( A< C >( 5.0f ) );
To get this implicit cast, I figured I could specialize A; something
along the lines of:
template<>
class A< C > {
...
public: A( float f ) { ... }
};
Now A< C > has a non-explicit constructor taking a float, and if C is
the only class used as a template parameter for A, then I had hoped the
compiler could unambiguously do the cast. Unfortunately, the compiler
looked right over it.
So, is it possible?
Any help appreciated!
-- Brian Byrne