c++ - 在嵌套类型中保留 volatile

标签 c++ templates

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);
}

f T推导为 volatile S<int> ,但是n是 只有int .我该怎么做才能保留 volatile , 那是, 有 nvolatile int

最佳答案

using n = typename std::conditional< std::is_volatile<T>::value, 
            volatile typename T::type,
            typename T::type >::type;

如果 Tvolatile,则将 volatile 添加到 n

关于c++ - 在嵌套类型中保留 volatile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54922795/

相关文章:

c++ - 如何在 OpenGL C++ 中旋转旋转轴?

c++ - 如何在 C++ operator() 中专门化调用模板?

c++ - 条件类型定义

html - Bootstrap - 制作响应式全宽模板,非响应式

c++ - 关于表达式副作用的 Clang 警告

c++ - 有什么方法可以知道线程创建成功还是失败?

c++ - C++ 中的无效间接

c++ - auto_ptr 莫名其妙的行为

c++ - VS 2011模板类

templates - 设计动态模板