Cpp templates functor that need get void or int - c++

How can I create a functor (in templates) that get void or int
template <class T>
Int TEMP::operator()(T s)
{
...
}
Thanks

Probably by not using templates:
template<class T> int TEMP<T>::operator()(int s)
{
...
}
template<class T> int TEMP<T>::operator()()
{
...
}

Related

Struct Template in C++

I have a header file,
class CListEx
{
public:
CListEx();
~CListEx();
void InitList();
void AddList(char * msg);
private:
template <typename T>
struct MyList {
T data;
int num;
};
};
Now in Cpp file,
void CListEx::InitList()
{
MyList *my = new MyList();
}
And this gives error,
E0441 argument list for class template "CListEx::MyList" is missing
Error C2514 'CListEx::MyList': class has no constructors
Any ideas?
What I TRIED:
I tried below approach and that also gives errors.
template <typename T >MyList *my = new template <typename T >MyList();
The error message gives a very clear reason: argument list for class template is missing. Meaning, you should write some code like:
void CListEx::InitList()
{
MyList<int> *my = new MyList<int>();
}
Read up on templates for example at: https://www.programiz.com/cpp-programming/templates
Is this what you are trying to do?
class CListEx
{
public:
template <typename T>
void InitList();
private:
template <typename T>
struct MyList {
T data;
int num;
};
};
template <typename T>
void CListEx::InitList()
{
MyList<T> *my = new MyList<T>();
}
int main()
{
CListEx cle;
cle.InitList<int>();
}
Or are you trying to do this?
template <typename T>
class CListEx
{
public:
void InitList();
private:
struct MyList {
T data;
int num;
};
};
template <typename T>
void CListEx<T>::InitList()
{
MyList *my = new MyList();
}
int main()
{
CListEx<int> cle;
cle.InitList();
}
Beware of the memory leak. Preferably use std::unique_prt from <memory>.

C++ template member function already defined with same type

I have this simple code below, a template with 2 type parameters. If I declare my class with the same type (like BidirectionalMap<int,int>), I receive an error:
int BidirectionalMap<T,S>::operator [](T) const' : member function already defined or declared
Here's my template code:
template <class T, class S>
class BidirectionalMap{
int count(T t){
return 1;
}
int count(S s){
return 1;
}
};
The error you got is normal, because after substitution you have
template <>
class BidirectionalMap<int, int>
{
int count(int t){ return 1; }
int count(int s){ return 1; } // Duplicated method
};
To solve that, you may provide partial specialization:
template <class T>
class BidirectionalMap<T, T>
{
int count(T t) { return 1; }
};
In C++20, you might use requires to "discard" methods:
template <class T, class S>
class BidirectionalMap
{
int count(T t) requires(!std::is_same<T, S>::value) { /*..*/ }
int count(S s) requires(!std::is_same<T, S>::value) { /*..*/ }
int count(T t) requires( std::is_same<T, S>::value) { /*..*/ }
};

Syntax error in C++ template member function

I'm new to C++ and would like to get some help with fixing the following template function code without removing the function fl()
template<type T>
class Test
{
int f1(T* x);
};
template< T>
int Test::f1(T* x)
{
return 5:
};
You have numerous syntax errors, but I guess your main issue is you need Test<T>::f1 instead of Test::f1:
//typename, not type
template<typename T>
class Test
{
int f1(T* x);
};
// forgot typename
template<typename T>
int Test<T>::f1(T* x)
//need ^^^
{
return 5;
}
//^ no semicolon
The correct syntax would be
template<typename T>
class Test
{
int f1(T* x);
};
template<typename T>
int Test<T>::f1(T* x)
{
return 5;
};
Note the keyword specifying the template argument T is either typename or class

error C2955 template+operator

I need some Help with this small code that includes re-def of operator and Template
But I keep getting this error:
error C2955 use of class template requires template argument list
#include<iostream>
using namespace std;
template <class T>
class student
{
friend int operator+(student &other1,student &other2);
public:
student(T g);
private:
int grade;
};
template <class T>
student<T>::student(T g)
{
grade=g;
}
void main()
{
student<int> s1(40),s2(90);
int sum;
sum=s1+s2;
cout<<"average="<<sum/2.0<<endl;
}//main
template <class T>
int operator+(student &other1,student &other2)
{
return other1.grade+other2.grade;
}
You need to say
template<class T>
int operator+(student<T> a, student<T> b)
...
You were missing the T

Overloading template function in cpp

I have this hpp
namespace A
{
template<class T>
class MyC
{
public:
T a;
};
template<class T>
void F(T r);
}
and this cpp
template<>
void A::F<double>(double r)
{
r;
}
template<>
void A::F<int>(int r)
{
r;
}
template<class T>
void A::F<A::MyC<T>>(A::MyC<T> r)
{
r;
}
template void A::F<A::MyC<int>>(A::MyC<int>);
template void A::F<A::MyC<double>>(A::MyC<double>);
but compiler says "unable to match function definition to an existing declaration" about F.
What's wrong with this code?
Put those declarations all in namespace A { ... } and remove A::. On the other hand, function template partial specialization is not allowed and this will make error:
template<class T>
void F<MyC<T>>(MyC<T> r)
{
...
}