如何允许通配符模板参数
How To Allow Wildcard Template Argument
我目前正在编写一个矩阵类,该类能够通过模板支持任意数量的行和列。我被困在如何防止无效操作(即矩阵 1 的列数≠矩阵 2 的行数(上。我当然可以存储行数和列数并在运行时进行检查,但最好是我想在编译时通过"通配符"模板参数进行此检查。
换句话说...
我想这样做:
template <typename T, int R, int C>
struct mat {
T matrix[R][C];
void operator *=(const mat<T, C, [can be anything]> &other) {
/* do operation */
}
};
取而代之的是:
template <typename T, int R, int C>
struct mat {
T matrix[R][C];
int rows = R;
int columns = C;
void operator *=(const mat *other) {
if (columns != other->rows) {
/* error */
} else {
/* do operation */
}
}
};
这可能吗?如果是这样,我该怎么做?
这对我有用
template <typename T, int R, int C>
struct mat {
T matrix[R][C];
template <int CC>
void operator *=(const mat<T, C, CC> &other) {
/* do operation */
}
};
int main()
{
mat<int, 2, 3> m1;
mat<int, 3, 4> m2;
m1 *= m2;
}
define template《int K》mat《T,R,K》&operator*(const mat《T, C,K》& other(。
相关文章:
- 表示"accepting anything for this template argument" C++概念的通配符
- 当使用通配符和null指针调用函数时,对输出的说明
- C++17 文件系统::remove_all 带有通配符路径
- 如何允许通配符模板参数
- 使用通配符的跨平台文件列表
- 如何检索由带通配符的字符串索引的对象
- 有效地查找通配符条目
- 具有不同输入和输出名称的生成文件通配符
- MinGW GCC通配符编译所有文件(Windows)
- Windows 上带有通配符的目录中的文件
- 查找第一个文件W通配符匹配
- 在某些情况下,通配符模式匹配失败
- 实现通配符后没有输入文件错误
- 如何为文件对话框过滤器设置通配符
- 使用通配符搜索QTableWidget时出现问题
- 为什么 std::experimental::filesystem::p ath 不接受像 "*" 这样的通配符?
- 在三元搜索树中搜索(NOT with)通配符
- SQLite如何在列上使用LIKE并添加通配符
- 带有通配符的 ifstream
- 通配符函数指针非类型模板参数