Template partial specialisation
Tag : cpp , By : eastcoastj
Date : March 29 2020, 07:55 AM
wish of those help Can somebody explain why are those two specializations indistinguishable to the compiler (gcc 4.5.1 @ ideone) , My understanding of the issue: typedef S<S<a, b>, c, d> S2;
|
partial template specialisation on template class and member function
Tag : cpp , By : Alex Sadzawka
Date : March 29 2020, 07:55 AM
hope this fix your issue Short answer: You can't explicitly specialize a member template of an class template that is not explicitly specialized. I guess using an overload as you suggested might be the simplest solution: #include <list>
#include <string>
struct Filter
{
// you constructor...
template < typename... T > Filter(T...){}
};
template<typename T>
class SQLObject
{
public:
template<typename U>
static std::list<T*> filter(const std::string& colum,
const std::string& ope,const U& value);
// v-here-v is the overload
static std::list<T*> filter(const std::string& colum,
const std::string& ope,
const std::string& value);
static std::list<T*> filter(const Filter& filter);
};
// works
template<typename T>
std::list<T*> SQLObject<T>::filter(const std::string& colum,
const std::string& ope,
const std::string& value)
{
// no to_string need whith std::string
return filter(Filter(colum,ope,value));
}
//[...]
std::string const& to_string(std::string const& p) { return p; }
// class definition etc.
template<typename T>
template<typename U>
std::list<T*> SQLObject<T>::filter(const std::string& colum,
const std::string& ope,const U& value)
{
//use to_string with all others types
using std::to_string;
return filter(Filter(colum,ope,to_string(value)));
}
|
Template partial specialisation not working with typedefs
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Inspired by this answer to a related question link, I tried the following and it seemed to work: template<typename T>
struct HasRootType
{
private:
typedef char no;
struct yes { no m[2]; };
static T* make();
template<typename U>
static yes check(U*, typename U::root_type* = 0);
static no check(...);
public:
static bool const value = sizeof(check(make())) == sizeof(yes);
};
template<typename T, bool = HasRootType<T>::value>
struct FindRootValueType
{
typedef typename T::root_type type;
};
template<typename T>
struct FindRootValueType<T, false>
{
typedef T type;
};
// define Base, A, B, C, etc here
|
Partial template member specialisation for arrays
Date : March 29 2020, 07:55 AM
it fixes the issue You can only specialize a single member function by providing all template arguments (you left the length as an argument), it would force you here to specify the array length. So if you need a specialized version for arrays of strings, leaving the length as a parameter, you need a specialization of the class template for array of strings first. template<class T>
class foo
{
public:
void tell() { cout << "general\n"; }
void base() { cout << "ok"; }
};
template<class T, std::size_t N>
class foo<T[N]>
{
public:
void tell() { cout << "special arrays\n"; }
};
template<std::size_t N>
class foo<std::string[N]>
{
public:
void tell() { cout << "special string arrays\n"; }
};
template<>
void foo<std::string>::tell()
{
cout << "string\n";
}
int main()
{
foo<std::string[2]> f;
f.tell();
return 0;
}
|
Partial template specialisation without rewriting the class
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Since your matrix class is templated on the height and width, a square matrix is a matrix with those template parameters equal. You can have such a class as a parameter easily with the same N for the second and third template argument: template <class T, int N>
auto get_det(const Matrix<T, N, N>& mat) -> float;
template <class T, int H, int W>
auto get_det(const Matrix<T, H, W>& mat) -> std::enable_if_t<H == W, float>;
template <class T, int H, int W>
struct Matrix {
template <int N = H, int M = W>
auto get_det() const -> std::enable_if_t<N == M, float> { return 2.4f; }
};
Matrix<int, 2, 3> not_square;
Matrix<int, 2, 2> square;
// error error: no matching member function for call to 'get_det'
// note: candidate template ignored: disabled by 'enable_if' [with N = 2, M = 3]
//not_square.get_det();
//OK:
square.get_det();
// No compilation error:
not_square.get_det<2,2>();
template <class T, int H, int W>
struct Matrix;
namespace detail {
template <class T, int N>
auto get_det(const Matrix<T, N, N>&) -> float { return 3.5; }
}
template <class T, int H, int W>
struct Matrix {
auto get_det2() const -> float { return detail::get_det(*this); }
};
|