Defining conversion operator for specialized template class only

Martin

I want to define conversion to float for matrix<1, 1>. I have trouble figuring out how to actually define it. If I make it a global function

template<typename T>
inline operator T(const matrix<T, 1, 1> &m){ return m(0, 0); }

I get "operator.. must be a non static member function"

I can of course define it as member for the generic matrix template but then it will be defined for all matrices - which is not what I want. I want it to be defined only for the specific case of 1x1 matrix.

Jarod42

You have to specialize a class for that, for example:

template <typename Base, typename T, std::size_t W, std::size_t H>
struct MatrixConversion
{ /*Empty*/ };

template <typename Base, typename T> struct MatrixConversion<T, 1u, 1u>
{
    operator const T&() const { return static_cast<const Base&>(*this).m[0][0]; }
};


template <typename T, std::size_t W, std::size_t H>
struct Matrix : MatrixConversion<Matrix<T, W, H>, T, W, H>
{
    // Your code
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Specialized constructors for a template class

From Dev

Operator Overload for Template Class Auto Type Conversion

From Dev

Template specialization within template specialized class

From Dev

Template class with template method specialized for itself

From Dev

Use generalized template class in specialized template functions

From Dev

Use generalized template class in specialized template functions

From Dev

Using a partial template class as a specialized template function

From Dev

Template class with template method specialized for itself

From Dev

why Specialized template class need forward declaration?

From Dev

template class with a single method specialized in C++

From Dev

using declaration of a specialized variadic template class

From Dev

using declaration of a specialized variadic template class

From Dev

Disable construction of non fully specialized template class

From Dev

C++11 Template Wrapper class with conversion operator - move semantics

From Dev

Defining conversion from a class to double

From Dev

Explicit specialization of a function template for a fully specialized class template

From Dev

Out-of-class definition of function of specialized inner class template?

From Dev

Passing a derived class to a template function specialized with base class

From Dev

Passing a derived class to a template function specialized with base class

From Dev

Template conversion operator priority & constness

From Dev

Conversion from template class

From Dev

operator = overload in template class

From Dev

Assignment operator of template class

From Dev

Template class with operator overload

From Dev

Overloading operator << in a template class

From Dev

C++ specialized template class for a given type list

From Dev

C++ Defining the << operator of an inner class

From Dev

C++ Defining the << operator of an inner class

From Dev

C++ Class Conversion Operator

Related Related

  1. 1

    Specialized constructors for a template class

  2. 2

    Operator Overload for Template Class Auto Type Conversion

  3. 3

    Template specialization within template specialized class

  4. 4

    Template class with template method specialized for itself

  5. 5

    Use generalized template class in specialized template functions

  6. 6

    Use generalized template class in specialized template functions

  7. 7

    Using a partial template class as a specialized template function

  8. 8

    Template class with template method specialized for itself

  9. 9

    why Specialized template class need forward declaration?

  10. 10

    template class with a single method specialized in C++

  11. 11

    using declaration of a specialized variadic template class

  12. 12

    using declaration of a specialized variadic template class

  13. 13

    Disable construction of non fully specialized template class

  14. 14

    C++11 Template Wrapper class with conversion operator - move semantics

  15. 15

    Defining conversion from a class to double

  16. 16

    Explicit specialization of a function template for a fully specialized class template

  17. 17

    Out-of-class definition of function of specialized inner class template?

  18. 18

    Passing a derived class to a template function specialized with base class

  19. 19

    Passing a derived class to a template function specialized with base class

  20. 20

    Template conversion operator priority & constness

  21. 21

    Conversion from template class

  22. 22

    operator = overload in template class

  23. 23

    Assignment operator of template class

  24. 24

    Template class with operator overload

  25. 25

    Overloading operator << in a template class

  26. 26

    C++ specialized template class for a given type list

  27. 27

    C++ Defining the << operator of an inner class

  28. 28

    C++ Defining the << operator of an inner class

  29. 29

    C++ Class Conversion Operator

HotTag

Archive