user3696944
user3696944

Reputation: 106

How to check if object is const or not?

My problem is that I have no idea how to check if object is const. I can only use C++98. How can I check if object have const modifier? How to overload functions properly?

int main(){
  Vec x;
  const Vec y;

  cout<<"Is x const? ";
  y.IfConst(x);     // cout << "no"
  cout<<"\n";

  cout<<"Is x const? ";
  x.IfConst(x)      // cout << "no"
  cout<<"\n";

  cout<<"Is y const? ";
  x.IfConst(y);     // cout << "yes"
  cout<<"\n";

  cout<<"Is y const? ";
  y.IfConst(y);     // cout << "yes"
  cout<<"\n";
  /**/
}

I need output look like: is x const? no is x const? no is y const? yes is y const? yes

I used:

void Vec::IsConst(Vec const &vecc) const{
  std::cout << "YES" << std::endl;      
}

void Vec::IsConst(Vec const &vecc) {
  std::cout << "NO" << std::endl;       
}

Upvotes: 7

Views: 7791

Answers (4)

user3696944
user3696944

Reputation: 106

To solve my problem i needed to overload functions

void Vec(Vec const &Vecc) const{
std::cout << "YES" << std::endl;            
}

void Vec(Vec const&Vecc){                        
std::cout << "YES" << std::endl;            
}

void Vec(Vec &Vecc) const {
std::cout << "NO" << std::endl;     
}

void Vec(Vec &Vecc) {
std::cout << "NO" << std::endl;     
}

Upvotes: 2

R Sahu
R Sahu

Reputation: 206607

Your proposed syntax does not make sense to me.

This works for me.

#include <iostream>

template <typename T> bool isConst(T& x)
{
   return false;
}

template <typename T> bool isConst(T const& x)
{
   return true;
}

int main() 
{
   int x;
   const double y = 0.0;

   std::cout << "Is x const? ";
   std::cout << isConst(x) << "\n";

   std::cout << "Is y const? ";
   std::cout << isConst(y) << "\n";
}

Output:

Is x const? 0
Is y const? 1

Upvotes: 2

quantdev
quantdev

Reputation: 23793

constness is known and used only as compile time, the information doesn't exist at runtime, it has no sense.

However, at compile time, if you have a C++11 compliant compiler, you can use the standard std::is_const type trait on a type:

int main() 
{
    std::cout << std::boolalpha;
    std::cout << std::is_const<const int>::value << '\n';
    std::cout << std::is_const<Vec>::value  << '\n';
}

If you don't have a c++11 compiler, you can use boost one.

Upvotes: 13

Spundun
Spundun

Reputation: 4034

You don't need to check for this at runtime. Since the compiler will throw an error if in the code you try to modify a const object.

On the other hand if you decide to do some typecasting/pointer magic to force modification of a const object then there is no way to detect it.

The main reason for the inability to detect is that, unlike interpreted languages like python and java(if you think of JVM as an interpreter), C++ doesn't provide much introspection features. structs and classes simply map their data layout as a block in memory, with no other metadata attached to it. This makes C/C++ the middle level language with higher efficiency and less introspection.

Upvotes: 1

Related Questions