如何检查分配给呼叫操作员结果的类型



我正在尝试制作一个简单的矩阵类

";mymat.h";

#ifndef _MYMAT_H_GUARD_
#define _MYMAT_H_GUARD_
#include <iostream>
constexpr auto MYMAT_ERR_UNEXPECTED_TYPE = "Error, unexpected type!";
constexpr auto MYMAT_ERR_CODE_UNEXPECTED_TYPE = 0;
constexpr auto MYMAT_ERR_OUT_OF_BOUND = "Error, out of bound!";
constexpr auto MYMAT_ERR_CODE_OUT_OF_BOUND = 0;
template <typename T>
class MYMAT{
public:
T* data;
int x, y;
public:
MYMAT(int x, int y);
~MYMAT();
template <typename C>
void set(int x, int y, C val);
template<typename C>
bool checkType(C val) const;

void print_mat();
public:
T& operator ()(int x, int y);
private:
bool inRange(int x, int y);
};
#endif // !_MYMAT_H_GUARD_
template<typename T>
inline MYMAT<T>::MYMAT(int x, int y){
this->data = new T[x * y]();
this->x = x;
this->y = y;
}
template<typename T>
inline MYMAT<T>::~MYMAT(){
delete this->data;
}
template<typename T>
inline void MYMAT<T>::print_mat(){
int x, y;
for (y = 0; y < this->y; y++)
{
for (x = 0; x < this->x; x++)
{
std::cout << this->data[y * this->x + x] << ' ';
}
std::cout << std::endl;
}
std::cout << std::endl;
}

template<typename T>
inline bool MYMAT<T>::inRange(int x, int y){
return !((x < 1) && (x > this->x) && (y < 1) && (y > this->y));
}

template<typename T>
template<typename C>
inline void MYMAT<T>::set(int x, int y, C val){
if (this->checkType(val)) {
if (this->inRange(x, y)) {
this->data[(y - 1) * this->x + (x - 1)] = val;
}
else {
std::cout << MYMAT_ERR_OUT_OF_BOUND;
exit(MYMAT_ERR_CODE_OUT_OF_BOUND);
}
}
else {
std::cout << MYMAT_ERR_UNEXPECTED_TYPE;
exit(MYMAT_ERR_CODE_UNEXPECTED_TYPE);
}
}

template<typename T>
inline T& MYMAT<T>::operator()(int x, int y)
{
return this->data[this->x * (y - 1) + (x - 1)];
}
template<typename T>
template<typename C>
inline bool MYMAT<T>::checkType(C val) const
{
return std::is_same_v<T, C>;
}

下面是我如何调用矩阵并使用set方法

#include <iostream>
#include "mymat.h"
int main()
{
MYMAT<int> m(3, 3);
m.set(2, 2, 500);
m.print_mat();
m.set(2, 2, 500.0);
m.print_mat();
}

它打印

0 0 00 500 00 0错误,意外类型

但当使用呼叫操作员时:

#include <iostream>
#include "mymat.h"
int main()
{
MYMAT<int> m(3, 3);
m(2, 2) = 500;
m.print_mat();
m(2, 2) = 500.0;
m.print_mat();
}

它打印:

0 0 00 500 00 00 00 500 00 0 0

如您所见,值从double转换为int。

如何将set()中的条件应用于呼叫接线员?

实现您想要的目标:

m(2, 2) = 500.0; // do custom checks for conversions from
// right hand side to left hand side

operator()返回T&是行不通的,因为您无法控制到T的隐式转换。在这种情况下,不能阻止从doubleint的转换。

相反,您可以从自己编写的operator()返回一个类型,这样您就可以对隐式转换进行所需的所有控制。此类型需要保留左侧的信息,即mthis指针,以及指向operator()的参数。它只需要支持operator=就可以从右侧检查隐式转换:

private:
struct Wrapper 
{
MYMAT *t;  // holds onto the this pointer
int x, y;

template <typename C>
void operator=(C val) 
{
t->set(x, y, val);  // uses MYMAT::set to do the conversion checking
}
};

现在你可以这样声明你的operator()

public:
Wrapper operator ()(int x, int y);

并这样定义:

template<typename T>
inline auto MYMAT<T>::operator()(int x, int y) -> Wrapper
{
return {this, x, y};
}

这是一个演示。

相关内容

最新更新