首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::add_const

Defined in header <type_traits>

template< class T > struct add_cv;

(1)

(since C++11)

template< class T > struct add_const;

(2)

(since C++11)

template< class T > struct add_volatile;

(3)

(since C++11)

提供成员类型胡枝子f。type这和T,但它的cv-限定符添加%28,除非T是一个函数,一个引用,或者已经有这个cv-限定符%29.

1%29加两者constvolatile...

2%29加const...

3%29加volatile...

成员类型

Name

Definition

type

the type T with the cv-qualifier

帮助者类型

template< class T > using add_cv_t = typename add_cv<T>::type;

(since C++14)

template< class T > using add_const_t = typename add_const<T>::type;

(since C++14)

template< class T > using add_volatile_t = typename add_volatile<T>::type;

(since C++14)

可能的实施

模板<class T>结构添加[医]CV<class T>结构添加[医]模板<class T>结构添加[医]易挥发的{tyduf挥发性T型;};

*。

二次

代码语言:javascript
复制
#include <iostream>
#include <type_traits>
 
struct foo
{
    void m() { std::cout << "Non-cv\n"; }
    void m() const { std::cout << "Const\n"; }
};
 
template <class T>
void call_m()
{
    T().m();
}
 
int main()
{
    call_m<foo>();
    call_m<std::add_const<foo>::type>();
}

二次

产出:

二次

代码语言:javascript
复制
Non-cv
Const

二次

另见

is_const (C++11)

checks if a type is const-qualified (class template)

is_volatile (C++11)

checks if a type is volatile-qualified (class template)

remove_cvremove_constremove_volatile (C++11)(C++11)(C++11)

removes const or/and volatile specifiers from the given type (class template)

as_const (C++17)

obtains a reference to const to its argument (function template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券