c++ - 如何检查分配给呼叫操作员结果的类型?
问题描述
我正在尝试制作一个简单的矩阵类
“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 0 0 500 0 0 0 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 0 0 500 0 0 0 0 0 0 0 0 500 0 0 0 0
如您所见,该值从 double 转换为 int。
如何在set()
呼叫运营商中应用条件?
解决方案
为了实现你想要的:
m(2, 2) = 500.0; // do custom checks for conversions from
// right hand side to left hand side
返回 a T&
fromoperator()
是行不通的,因为您无法控制隐式转换为T
. 在这种情况下,您无法阻止从double
到的转换int
。
相反,您可以从operator()
自己编写的类型中返回一个类型,这样您就可以控制隐式转换。这种类型需要保留左侧的信息,即 的this
指针m
和 的参数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};
}
这是一个演示。
推荐阅读
- java - 如何解决 Neo4J-admin 导入中的未排序数据错误
- c++ - selectedIndexes() 返回的 QModelIndexList 始终为空
- javascript - 从 LAN 中的其他设备访问我的 ReactJS 项目
- c# - c# nunit extent 报告没有截屏,传递异常
- r - 如何提取列表中所有唯一因子水平的排列
- audiokit - 如何播放一次 AKAppleSequencer 的一个部分/范围
- verilog - FIFO的verilog代码(先进先出)没有显示正确的结果?
- java - 错误:JMSCC5007:不支持使用 JMS2.0 API“createContext()”
- django - 更改语言后如何重定向到上一页?
- android - React Native Release apk 在 RazorpayCheckout.open razorpay 网关上崩溃