c++ - const and its position w/o pointers or references are present

In C++, the poisition of const has special rules with/without the pointer is present.

 

Let's first see some examples that shows that the position of the const is irrelevant in terms of reference or ordinal Type declaration without pointer.

 

 

let 's see the example :

 

suppose we have function template, which is as follow. 

 

int main3(int argc, _TCHAR* argv[]) { 

	// it does not matter if you have int const or const int
    const int c_a = 0;
	int const c_a2 = 0;

	// nor does it matter if you have const int & or int const & 
	const int &r_ca = c_a;
	int const &r_ca2 = c_a;

	// the thing that get tricker is when the pointer is involved
	// ponter to array
	int *p_ia = ia;
	// point to const array
	const int *p_cia = ia;

	// reference to pointer to const array
	const int *&r_cia = p_cia;
	// reference to pointer to const array
	int const *&r_cia2 = p_cia;

	// const reference to pointer to array
	int * const &r_cia3 = p_ia;
	//int * &const r_cia4 = p_ia; // this is a compilation warning, chronical use error, wrong qualification

	// const reference to pointer to const array
	const int * const & r_cia4 = p_cia;

	// you cannot change the value of r_cia3 and r_cia4
	//r_cia3 = 0; // error: you cannot assign to const 
	//r_cia4 = 0; // error: you cannot assign to const

	return 0;
}
 

 

So as you can see, it does not matter if  you have int const of const int, nor does it matter if you have const int & or int const &;

 

Given the following function template definition;

 

template<typename Type, int size>
Type min(Type (&r_arr)[size]) { 
	Type min_val = r_arr[0];
	for (int i = 1; i < size; i++) { 
		if (r_arr[i] < min_val) {
			min_val = r_arr[i];
		}
	}

	return min_val;
}
 

 

Suppose that you want to manifest that there is no change to the array and the return value is a rvalue, then  you can add the const modifier 

 

template<typename Type, int size>
const Type min(const Type (&r_arr)[size]) { 
	Type min_val = r_arr[0];
	for (int i = 1; i < size; i++) { 
		if (r_arr[i] < min_val) {
			min_val = r_arr[i];
		}
	}

	return min_val;
}
 

but some readers may wonder what if I add in addition a reference symbols, such as this: 

 

template<typename Type, int size>
const Type&  min(const Type& (&r_arr)[size]) { 
	Type min_val = r_arr[0];
	for (int i = 1; i < size; i++) { 
		if (r_arr[i] < min_val) {
			min_val = r_arr[i];
		}
	}

	return min_val;
}
 

It won't compile as the error:

 

"r_arr: reference to reference is illegal"

"r_arr: array of reference is illegal"

 

 

as many user may have found out, as it does not make difference to have "const Type &" or "Type const &", so the following declaration is also wrong. 

 

template<typename Type, int size>
Type const&  min(Type const& (&r_arr)[size]) { 
    // ... 
}
 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/v1/object.hpp:664:34: error: ‘void* memcpy(void*, const void*, size_t)’ copying an object of non-trivial type ‘struct msgpack::v2::object’ from an array of ‘const msgpack_object’ {aka ‘const struct msgpack_object’} [-Werror=class-memaccess] std::memcpy(&o, &v, sizeof(v)); ^ In file included from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/object_fwd.hpp:17, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/v1/adaptor/adaptor_base_decl.hpp:14, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/adaptor/adaptor_base_decl.hpp:13, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/adaptor/adaptor_base.hpp:13, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/v1/object_decl.hpp:16, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/object_decl.hpp:14, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/object.hpp:13, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack.hpp:10, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/example/cpp03/stream.cpp:10: /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/v2/object_fwd.hpp:23:8: note: ‘struct msgpack::v2::object’ declared here struct object : v1::object { ^~~~~~ cc1plus: all warnings being treated as errors make[2]: *** [example/cpp03/CMakeFiles/stream.dir/build.make:63:example/cpp03/CMakeFiles/stream.dir/stream.cpp.o] 错误 1 make[2]: 离开目录“/home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master” make[1]: *** [CMakeFiles/Makefile2:415:example/cpp03/CMakeFiles/stream.dir/all] 错误 2 make[1]: 离开目录“/home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master”
最新发布
07-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值