简体   繁体   English

c ++编译时检查函数参数

[英]c++ compile-time check function arguments

I'm searching a way to check function arguments in compile-time if it's possible to do for compiler. 如果可以为编译器做的话,我正在寻找一种在编译时检查函数参数的方法。

To be more specific: assume that we have some class Matrix. 更具体一点:假设我们有一些类Matrix。

class Matrix
{
    int x_size;
    int y_size;

public:
    Matrix(int width, int height):
        x_size{width},
        y_size{height}
    {}
    Matrix():
        Matrix(0, 0)
    {}
};

int main()
{
    Matrix a; // good.
    Matrix b(1, 10); // good.
    Matrix c(0, 4); // bad, I want compilation error here.
}

So, can I check or differentiate behavior (function overloading?) in case of static (source-encoded) values passed to function? 那么,在传递给函数的静态(源编码)值的情况下,我可以检查或区分行为(函数重载?)?

If value isn't static: 如果值不是静态的:

std::cin >> size;
Matrix d(size, size);

we're only able to do runtime checks. 我们只能做运行时检查。 But if values are encoded in source? 但是如果值在源代码中编码? Can I make compile-time check in this case? 在这种情况下我可以进行编译时检查吗?

EDIT: I think this can be possible with constexpr constructor , but anyway overloading with and without constexpr isn't allowed. 编辑:我认为这可以通过constexpr构造函数实现 ,但无论如何都不允许使用和不使用constexpr进行重载。 So problem can't be resolved in way I suppose. 所以问题无法以我想象的方式解决。

To get a compile time error you would need a template: 要获得编译时错误,您需要一个模板:

template <int width, int height>
class MatrixTemplate : public Matrix
{
    static_assert(0 < width, "Invalid Width");
    static_assert(0 < height, "Invalid Height");
    public:
    MatrixTemplate()
    : Matrix(width, height)
    {}
};

(Btw.: I suggest unsigned types for indices) (顺便说一句:我建议索引的无符号类型)

If you do not have static_assert (here I switch to unsigned): 如果你没有static_assert(这里我切换到unsigned):

template <unsigned width, unsigned height>
class MatrixTemplate : public Matrix
{
    public:
    MatrixTemplate()
    : Matrix(width, height)
    {}
};

template <> class MatrixTemplate<0, 0> {};
template <unsigned height> class MatrixTemplate<0, height> {};   
template <unsigned width> class MatrixTemplate<width, 0> {};

There is no support for empty matrices (MatrixTemplate<0, 0>), here. 这里不支持空矩阵(MatrixTemplate <0,0>)。 But it should be an easy task to adjust the static_asserts or class MatrixTemplate<0. 但是调整static_asserts或类MatrixTemplate <0应该是一件容易的事。 0>. 0>。

You may add a method like this: 您可以添加如下方法:

template <int WIDTH, int HEIGHT>
Matrix CreateMatrix()
{
    static_assert(WIDTH > 0, "WIDTH > 0 failed");
    static_assert(HEIGHT > 0, "HEIGHT > 0 failed");

    return Matrix(WIDTH, HEIGHT);
}

int main() {
    Matrix m(0, 2);     // no static check
    Matrix m2 = CreateMatrix<0,2>(); // static check

    return 0;
}

The way that linear algebra packages tend to do this is to use templates for fixed size matrices, as in: 线性代数包倾向于这样做的方式是使用固定大小矩阵的模板,如:

template<int x, int y> class Matrix { ... }

and an extra class for matrices that can change size at runtime 和一个可以在运行时改变大小的矩阵的额外类

class DynamicMatrix {...}

You still have to rely on the programmer actually using the first option when they want fixed size matrices, but the template version makes it easy to generate a compiler error when x or y are zero. 当他们想要固定大小的矩阵时,您仍然必须依赖程序员实际使用第一个选项,但是当xy为零时,模板版本可以很容易地生成编译器错误。

Run-time: 运行:

Matrix(int width, int height):
    x_size{width},
    y_size{height}
{
        assert(x_size>0);
        assert(y_size>0);
}

Compile-time (Actually you couldn't do it with function arguments. You can use template ways): 编译时(实际上你无法使用函数参数。你可以使用模板方式):

template <size_t WIDTH, size_t HEIGHT>
class Matrix
{
    const size_t x_size = WIDTH;
    const size_t y_size = HEIGHT;

    static_assert(WIDTH > 0, "Width must > 0");
    static_assert(HEIGHT > 0, "Height must > 0");
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用带有参数类型的可变参数模板在c ++中获取函数在编译时的参数个数 - Getting the number of arguments to a function at compile-time using variadic templates with argument type check in c++ C ++自定义编译时检查 - C++ custom compile-time check c / c ++编译时“兼容性”检查 - c / c++ compile-time “compatibility” check C ++编译时检查是否可以使用某种类型的参数调用重载函数 - C++ Compile-time check that an overloaded function can be called with a certain type of argument 用于检查所有模板参数类型是否唯一的编译时 C++ 函数 - Compile-time C++ function to check whether all template argument types are unique 在C ++中编译时接口实现检查 - Compile-time interface implementation check in C++ C ++中虚函数的编译时静态类型检查 - Compile-time static type check of virtual functions in C++ 使用注释在编译时自动包装C / C ++函数 - Automatically wrap C/C++ function at compile-time with annotation C ++中的编译时条件成员函数调用 - compile-time conditional member function call in C++ C++:编译时检查匹配的 function 调用对? - C++: compile-time checking for matching pairs of function calls?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM