C++ 在嵌套类型中保留volatile

C++ 在嵌套类型中保留volatile,c++,templates,c++17,C++,Templates,C++17,在f中,T被推导为挥发性S,而n是 只有int。我要做什么来保持挥发性,也就是, 有n是不稳定的int吗 如果T为volatile,则将volatile添加到n 如果T是挥发性的,则将挥发性添加到n。用于funsies。如果您需要经常做这类事情,可以将其封装在元函数中。以下是一个可能的实现: 它是SFINAE友好的,因此如果所传递的类型没有::type成员,它也不会有::type成员。否则,它将通过转发限定符来公开相同的类型 当应用于您的示例时。用于趣味。如果您需要经常做这类事情,可以将其封装在

在f中,T被推导为挥发性S,而n是 只有int。我要做什么来保持挥发性,也就是, 有n是不稳定的int吗

如果T为volatile,则将volatile添加到n


如果T是挥发性的,则将挥发性添加到n。

用于funsies。如果您需要经常做这类事情,可以将其封装在元函数中。以下是一个可能的实现:

它是SFINAE友好的,因此如果所传递的类型没有::type成员,它也不会有::type成员。否则,它将通过转发限定符来公开相同的类型


当应用于您的示例时。

用于趣味。如果您需要经常做这类事情,可以将其封装在元函数中。以下是一个可能的实现:

它是SFINAE友好的,因此如果所传递的类型没有::type成员,它也不会有::type成员。否则,它将通过转发限定符来公开相同的类型


当应用于您的示例时是。

您怎么知道它不是?volatile限定符只用于s,不用于s::type。@user463035818是的,f函数的T是volatile s。但是s中的T是int。@Someprogrammerdude啊,对了,我有点困惑,type不是成员变量,只是typedefs;没有什么意义S是一种特性。此外,volatile很少是你想要的。你怎么知道它不是呢?volatile限定符只用于s,不用于s::type。@user463035818是的,f函数的T是volatile s。但是s中的T是int。@Someprogrammerdude啊,对了,我有点困惑,type不是一个成员变量,只是一个typedefs volatile;没有什么意义S是一种特性。此外,volatile很少是您想要的。
template<typename T>
struct S {
    using type = T;
};

volatile S<int> s;

template<typename T>
void f(T& v) {
    using n = typename T::type;

    S<n>::_; // to show

}

int main() {
     f(s);
}
using n = typename std::conditional< std::is_volatile<T>::value, 
            volatile typename T::type,
            typename T::type >::type;
#include <type_traits>

template<class Trait, typename=void>
struct propogate_cv_to_type{};

template<class Trait>
struct propogate_cv_to_type<Trait, std::void_t<typename Trait::type>>
{ using type = typename Trait::type; }; 

template<class Trait>
struct propogate_cv_to_type<Trait const, std::void_t<typename Trait::type>>
{ using type = typename Trait::type const; }; 

template<class Trait>
struct propogate_cv_to_type<Trait volatile, std::void_t<typename Trait::type>>
{ using type = typename Trait::type volatile; }; 

template<class Trait>
struct propogate_cv_to_type<Trait const volatile, std::void_t<typename Trait::type>>
{ using type = typename Trait::type const volatile; };