简体   繁体   English

检查变量是否为常量限定

[英]Checking if a variable is constant qualified

I was reading about const_cast and it seemed unsafe and not very helpful.我正在阅读有关 const_cast 的内容,它似乎不安全且没有太大帮助。 The best answer about it in SO states the it would be useful in a scenario like this: SO中关于它的最佳答案指出它在这样的场景中很有用:

void func(const char* param, bool modify){
    if(modify)
        //const_cast and change param
    // stuff that do not change param
}

Although this a possible use, it has its risks because you have to correctly provide the value of "modify", or else you will get an undefined behaviour since you are changing something that was supposed to be constant.虽然这是一种可能的用途,但它有其风险,因为您必须正确提供“修改”的值,否则您将获得未定义的行为,因为您正在更改本应保持不变的内容。 I wonder if you could achieve the same functionality without having to supply this extra argument and for that you would most likely need to check the existence of a const qualifier.我想知道您是否可以在无需提供这个额外参数的情况下实现相同的功能,为此您很可能需要检查 const 限定符的存在。

The closes thing I found was the std function is_const , but it seems to be limited to a different kind of usage:我发现的关闭的是 std 函数is_const ,但它似乎仅限于不同类型的用法:

is_const<const int>::value //returns true
is_const<int>::value // returns false
const int myVar=1;
is_const<myVar>::value // what it would look like ( does not compile)

I've also tried using similar function signatures that only differ by a "const" qualifier, but that is perceived as a redefinition.我也尝试过使用类似的函数签名,它们仅在“const”限定符上有所不同,但这被视为重新定义。 So is it possible at all to do it?那么有可能做到吗? If so, how can it be done?如果是这样,怎么做?

You can use std::is_const<decltype(myVar)>::value to check whether myVar has been declared as const .您可以使用std::is_const<decltype(myVar)>::value来检查myVar是否已声明为const

However, if myVar is a pointer or a reference to another object, there is no way of knowing if that object is const or not from inside the function.但是,如果myVar是另一个对象的指针或引用,则无法从函数内部知道该对象是否为const

For this case, I think you can just use two overloads of the function:对于这种情况,我认为您可以只使用该函数的两个重载:

#include <stdio.h>

void func(const char* param) {
    printf("Not modifying %c\n", param[0]);
}

void func(char* param) {
    printf("Modifying %c\n", param[0]);
    func(const_cast<const char*>(param));
}

int main() {
    const char foo[1] = {'F'};
    char bar[1] = {'B'};

    func(foo);
    func(bar);
}

Which outputs:哪些输出:

Not modifying F
Modifying B
Not modifying B

When calling func , the right overload is automatically selected based on the const-ness of the argument.调用func ,会根据参数的常量性自动选择正确的重载。 Here, I let the non-const version call the const version to do the non-const part of the work, which seemed to fit the original usecase, but you can of course also let both of these call a third (possibly private/static) version that takes a bool modify parameter as original suggested, but now in the knowledge that the constness is correct).在这里,我让非常量版本调用 const 版本来完成非常量部分的工作,这似乎适合原始用例,但是您当然也可以让这两个调用第三个(可能是私有/静态的) ) 版本采用原始建议的bool modify参数,但现在知道常量是正确的)。

Alternatively, if you want to use std::is_const , you can use a template function to autogenerate versions for const and non-const and automatically deduct the modify parameter:或者,如果您想使用std::is_const ,您可以使用模板函数为 const 和非常量自动生成版本并自动扣除修改参数:

#include <stdio.h>
#include <type_traits>

static void func(char* param, bool modify) {
    if (modify)
        printf("Modifying %c\n", param[0]);
    printf("Not modifying %c\n", param[0]);
}

template<typename T>
void func(T *param) {
    func(const_cast<char*>(param), !std::is_const<T>::value);
}

With the same main() as in the first example, this produces exactly the same output as the first example.使用与第一个示例相同的main() ,这将产生与第一个示例完全相同的输出。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM