I am trying to use enable_if in boost to do template specialization, but cannot get it to work and confused as to how to write it and what the syntax actually means. I've read the boost docs but still doesn't quite make sense to me
I have four methods which i want all to be named the same thing and are all very similar. I have two functions that will return a string and two functions that will return an integer. In each case the parameter are int,int,return type or int,const char*,return type since the last parameter will be a default value that can be returned.
so something like this...
int getVal(int,int,int)
int getVal(int,const char*,int)
string getVal(int,int,string)
string getVal(int,const char*,string)
but this will not work since the parameters are the same and only the return type differs and i need it to be a templated method rather just an overloaded method. This is in a non-templated class. so i need to enable_if if the return type is a string or an int, or maybe check enable_if on the last parameter?
Any help would be appreciated, if someone would be able to explain what the syntax means and is actually doing that would be helpful as well. Thanks
this is one of the many attempts i tried in getting the correct syntax..
template<typename myType>
typename enable_if<boost::is_arithmetic<myType>, myType>::type
GetValue(int row, int col, myType d)
{
unsigned int columnIndex = this->GetColumnIndex(col);
try {
CheckColumnType(col, Integer);
}
catch(DatatableException e){
return d;
}
if("0" == this->m_rows[row][col])
{
return 0;
}
else if("1" == this->m_rows[row][col])
{
return 1;
}
else
{
return atoi(this->m_rows[row][col].c_str());
}
}
template<typename myType>
typename disable_if<boost::is_arithmetic<myType>, myType>::type
GetValue(int row, int col, myType d)
{
unsigned int columnIndex = this->GetColumnIndex(col);
try {
CheckColumnType(col, String);
}
catch(DatatableException e){
return d;
}
return this->m_rows[row][col];
}
template <class T>
T GetValue(int row, typename enable_if<is_arithmetic<!T> >::type* dummy = 0){
{
unsigned int columnIndex = this->GetColumnIndex(col);
try {
CheckColumnType(col, Integer);
}
catch(DatatableException e){
return d;
}
if("0" == this->m_rows[row][col])
{
return 0;
}
else if("1" == this->m_rows[row][col])
{
return 1;
}
else
{
return atoi(this->m_rows[row][col].c_str());
}
}
template <class T>
T GetValue(int row, typename enable_if<is_arithmetic<T> >::type* dummy = 0){
try {
CheckColumnType(col, Integer);
}
catch(DatatableException e){
return d;
}
if("0" == this->m_rows[row][col])
{
return 0;
}
else if("1" == this->m_rows[row][col])
{
return 1;
}
else
{
return atoi(this->m_rows[row][col].c_str());
}
}
(int, int, int)
,(int, const char*, int)
,(int, int, string)
,(int, const char*, string)
– Rostint ret = getVal(1,1);
will not compile if the third parameter in each function has a default argument. – cschwan