Divya Aggarwal
Divya Aggarwal

Reputation: 61

Qualifiers for a variable in C++

How do I know if qualifiers like const etc are associated with the passed arguments to a function?

e.g.

template<class T>
void callback(T & data)
{
  body of function
}

How do I know if the data is const etc?

Upvotes: 0

Views: 78

Answers (1)

eerorika
eerorika

Reputation: 238401

You can test whether a type (including a template type argument) is const qualified using a standard type trait:

bool is_const = std::is_const_v<T>;

If T is const qualified, then and only then is T& a reference to const.

Whether or not the referred object is const is something that cannot be inspected.

Upvotes: 1

Related Questions