I am making a little experiment to test const-ness of data referred to by shared pointers, and of the pointers themselves. I wrote the following code:
#include <iostream>
#include <vector>
#include <memory>
#include <type_traits>
using std::cout;
using std::endl;
using std::vector;
using std::shared_ptr;
auto testfun(const shared_ptr<const vector<int>> &vec){
cout<<std::is_const<const shared_ptr<const vector<int>>&>::value<<endl; // not const?
cout<<std::is_const<decltype(vec)>::value<<endl; // not const?
cout<<std::is_const<decltype(*vec)>::value<<endl; // not const?
cout<<std::is_const<decltype(&vec)>::value<<endl; // not const?
}
int main(){
const vector<int> myvec = {3,3,3,3,3,3};
const shared_ptr<const vector<int>> vec =
std::make_shared<const vector<int>>(myvec);
cout<<std::is_const<decltype(vec)>::value<<endl; // is const
testfun(vec);
return 0;
}
The output of this code is:
1
0
0
0
0
Can someone explain why the last four checks do not qualify as const? I am especially curious as to why std::is_const<decltype(vec)>
does not have const
type traits.