抑制单行 MSVC19 C++20 的编译器错误

发布于 2025-01-13 13:14:03 字数 1919 浏览 1 评论 0原文

我想检查 shared_ptr 是否未初始化,即它是否是 nullptr 或者它是否有默认值(如果使用 创建的话就是这种情况) std::make_shared)。为此,我编写了一段代码来检查共享指针是否具有类/结构的默认构造值。为了实现这一点,我还需要知道类/结构是否定义了 operator== 并确定我有 has_equate。检查共享指针是否未初始化的代码是:

template<class L, class R = L>
concept has_equate = requires(const L & lhs, const R & rhs)
{
    { lhs == rhs } -> std::same_as<bool>;
};

template <typename T>
bool isSharedPtrUninitialized(const std::shared_ptr<T>& shared)
{
    if(shared) {
        if(has_equate<T>) {
            T tmp;
            if(*shared == tmp)
                return true;
        }
        return false;
    }
    return true;
}

如果我没有为我的类/结构定义 operator== ,上面的代码行会给出以下错误:

错误 C2678 二进制“==”:找不到采用“T”类型左侧操作数的运算符(或者没有可接受的转换)

因为我正在检查 operator== 是否存在对于类/结构,在继续检查给定共享指针是否具有默认构造值之前使用 has_equate ,我想抑制此特定代码行的此错误。

到目前为止我已经尝试过:

template <typename T>
bool isSharedPtrUninitialized(const std::shared_ptr<T>& shared)
{
    if(shared) {
        if(has_equate<T>) {
            T tmp;
            #pragma warning(suppress: 2678)
            if(*shared == tmp)
                return true;
        }
        return false;
    }
    return true;
}
#pragma warning(push)
#pragma warning(disable: 2678)
template <typename T>
bool isSharedPtrUninitialized(const std::shared_ptr<T>& shared)
{
    if(shared) {
        if(has_equate<T>) {
            T tmp;
            if(*shared == tmp)
                return true;
        }
        return false;
    }
    return true;
}
#pragma warning(pop)

没有一个有效(我认为这是因为 C2678 是一个错误而不是警告)。有没有办法可以仅针对 if(*shared == tmp) 代码行或仅针对 isSharedPtrUninitialized 函数抑制此错误,因为我不想这样做对于整个事情。

I want to check if a shared_ptr is uninitialized, i.e. if it is a nullptr or if it has a default value (as would be the case if created by using std::make_shared). For this purpose I wrote a code to check if the shared pointer is having a default constructed value of a class/struct. To achieve this, I also need to know if a class/struct defines the operator== and to figure that out I have has_equate. The code to check if a shared pointer is uninitialized is:

template<class L, class R = L>
concept has_equate = requires(const L & lhs, const R & rhs)
{
    { lhs == rhs } -> std::same_as<bool>;
};

template <typename T>
bool isSharedPtrUninitialized(const std::shared_ptr<T>& shared)
{
    if(shared) {
        if(has_equate<T>) {
            T tmp;
            if(*shared == tmp)
                return true;
        }
        return false;
    }
    return true;
}

If I don't define operator== for my class/struct the above line of code gives the following error:

Error C2678 binary '==': no operator found which takes a left-hand operand of type 'T' (or there is no acceptable conversion)

Since I am checking if the operator== exists or not for the class/struct by using has_equate before proceeding to check if the given shared pointer has a default constructed value, I would like to suppress this error for this particular line of code.

What I have tried so far:

template <typename T>
bool isSharedPtrUninitialized(const std::shared_ptr<T>& shared)
{
    if(shared) {
        if(has_equate<T>) {
            T tmp;
            #pragma warning(suppress: 2678)
            if(*shared == tmp)
                return true;
        }
        return false;
    }
    return true;
}
#pragma warning(push)
#pragma warning(disable: 2678)
template <typename T>
bool isSharedPtrUninitialized(const std::shared_ptr<T>& shared)
{
    if(shared) {
        if(has_equate<T>) {
            T tmp;
            if(*shared == tmp)
                return true;
        }
        return false;
    }
    return true;
}
#pragma warning(pop)

None of it is working (I assume it is because C2678 is an error and not a warning). Is there a way I could suppress this error for just if(*shared == tmp) line of code or just for the isSharedPtrUninitialized function as I do not want to do this for the entire thing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱,才寂寞 2025-01-20 13:14:03

感谢 @molbdnilo 提供解决方案。工作功能是:

template <typename T>
bool isSharedPtrUninitialized(const std::shared_ptr<T>& shared)
{
    if(shared) {
        if constexpr (has_equate<T>) {
            T tmp;
            if(*shared == tmp)
                return true;
        }
        return false;
    }
    return true;
}

使用 if constexpr 而不是 if 为我解决了问题。如果对这两者之间的区别感到好奇,请查看帖子“if constexpr()”与“if()”之间的区别< /a> 就这样。

Thanks to @molbdnilo for providing the solution. The working function is:

template <typename T>
bool isSharedPtrUninitialized(const std::shared_ptr<T>& shared)
{
    if(shared) {
        if constexpr (has_equate<T>) {
            T tmp;
            if(*shared == tmp)
                return true;
        }
        return false;
    }
    return true;
}

Using if constexpr instead of if solved the problem for me. If curious about the difference between these two then have a look at the post Difference between "if constexpr()" Vs "if()" on SO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文