保存宪法

发布于 2025-02-12 10:12:18 字数 1158 浏览 1 评论 0原文

如果类型是指针,const等,我想要一种保存的方法。请参阅以下内容。

template<typename type>
void test(type t) {
    std::cout << "t const? = " << std::is_const<decltype(t)>() << std::endl;
    int x = (int) t;
    std::cout << "x const? = " << std::is_const<decltype(x)>() << std::endl;
};

int main() {
    const int x = 0;
    int y = 0;

    test<const int>(x);
    test<int>(y);
}
>>> t const? = 1
>>> x const? = 0
>>> t const? = 0
>>> x const? = 0

我该如何制作以使函数打印以下内容?换句话说,我如何做到这一点,以便如果模板参数为const,则x也是const?

>>> t const? = 1
>>> x const? = 1
>>> t const? = 0
>>> x const? = 0

我想避免执行以下操作。

// bad
if constexpr (std::is_const<decltype(t)>()) {
    // do const cast
} else {
    // do non const cast
}

我拥有的具体情况是我有一个const或非const void指针,并希望将其施加给具有类型的指针,同时保存构造。

编辑:这是一个糟糕的例子。您可以使用类型X =(类型)T。我想要使​​用类型特征的解决方案,因为这对我来说不是有效的解决方案。

I want a way of preserving if the type is a pointer, const, etc upon a cast. See the following.

template<typename type>
void test(type t) {
    std::cout << "t const? = " << std::is_const<decltype(t)>() << std::endl;
    int x = (int) t;
    std::cout << "x const? = " << std::is_const<decltype(x)>() << std::endl;
};

int main() {
    const int x = 0;
    int y = 0;

    test<const int>(x);
    test<int>(y);
}
>>> t const? = 1
>>> x const? = 0
>>> t const? = 0
>>> x const? = 0

How do I make it so that the function prints the following? In other words, how do I make it so that if the template argument is const, x is also const?

>>> t const? = 1
>>> x const? = 1
>>> t const? = 0
>>> x const? = 0

I would like to avoid having to do anything like the following.

// bad
if constexpr (std::is_const<decltype(t)>()) {
    // do const cast
} else {
    // do non const cast
}

The specific scenario I have is I have a const or non-const void pointer and want to cast it to a pointer with a type, whilst preserving the constness.

Edit: This is a poor example. You can use type x = (type) t. I wanted a solution using type traits because this isn't a valid solution for me.

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

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

发布评论

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

评论(1

碍人泪离人颜 2025-02-19 10:12:18

您的示例不是最好说明施放void*的实际问题,可能是const,以相同的constness,因为您可以简单地用type x = t;替换该行以获取所需的输出。

但是,您可以使用一种类型特征:

#include <iostream>
#include <type_traits>

template <typename From,typename To>
struct preserve_const {
    using type = std::remove_const<To>::type;
};

template <typename From,typename To>
struct preserve_const<const From,To> {
    using type = const To;
};

template <typename From,typename To>
using preserve_const_t = typename preserve_const<From,To>::type;

template<typename type>
void test(type t) {
    std::cout << "t const? = " << std::is_const<decltype(t)>() << std::endl;
    preserve_const_t<type,int> x = t;  
    std::cout << "x const? = " << std::is_const<decltype(x)>() << std::endl;
};

int main() {
    const int x = 0;
    int y = 0;

    test<const int>(x);
    test<int>(y);
}

或如评论中提到的,也许更简单:

template <typename type,typename T>
using the_type = std::conditional_t< std::is_const_v<type>,const T,T>;

Your example isnt the best to illustrate the actual issue to cast a void*, possibly const, to some T* with same constness, because you can simply replace the line with type x = t; to get desired output.

However, you can use a type trait:

#include <iostream>
#include <type_traits>

template <typename From,typename To>
struct preserve_const {
    using type = std::remove_const<To>::type;
};

template <typename From,typename To>
struct preserve_const<const From,To> {
    using type = const To;
};

template <typename From,typename To>
using preserve_const_t = typename preserve_const<From,To>::type;

template<typename type>
void test(type t) {
    std::cout << "t const? = " << std::is_const<decltype(t)>() << std::endl;
    preserve_const_t<type,int> x = t;  
    std::cout << "x const? = " << std::is_const<decltype(x)>() << std::endl;
};

int main() {
    const int x = 0;
    int y = 0;

    test<const int>(x);
    test<int>(y);
}

Or as mentioned in comments and perhaps much simpler:

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