`

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

    博客分类:
  • c++
c++ 
阅读更多

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]) { 
    // ... 
}
 

 

 

 

分享到:
评论

相关推荐

    c++中const关键字使用详解

    C++语言中的const关键字是一个非常重要的修饰符,它的核心作用是声明一个变量为常量,即不可修改。正确地理解和使用const,可以帮助提高程序的健壮性和安全性。本文将详细介绍const关键字在C++中的使用方法。 一、...

    Matlab2020a配置MinGW-w64 C/C++ 编译器

    在使用Matlab进行C或C++编程时,有时我们需要将代码编译为MEX文件,以便于Matlab与C/C++代码之间的高效交互。Matlab自带的编译器支持多种环境,包括Microsoft Visual Studio和MinGW-w64。本教程将聚焦于如何在Matlab...

    C++中const用法全解

    C++ 中 const 用法全解 const 在 C++ 中占有重要作用,属于小兵立大功的典型,本文档详细介绍了如何使用 const。 1. const 常量 在 C++ 中,const 可以用来修饰变量,称为常量。例如:const int max = 100; 这种...

    C++ - Tracebin Utility - A binary tracer

    static const unsigned int step = 16; private: static char viewChar (const char ch); static std::string viewHead (int i); static std::string viewHexes (const std::string &str;); static std::...

    c++ 的array源码分析和reverse-iterator和-Array-const-iterator类

    c++ 的array源码分析和reverse-iterator和-Array-const-iterator类

    C++中const用法总结

    ### C++中const用法总结 #### 一、引言 `const` 是 C++ 中一个非常重要的关键字,用于声明常量或指定某些实体不可被修改。熟练掌握 `const` 的使用方法对于编写高质量的 C++ 代码至关重要。本文将详细介绍 `const`...

    C++中const用法总结.doc

    在C++编程语言中,`const`关键字是一个非常重要的元素,用于声明不可变性,确保程序的稳定性和安全性。以下是对C++中`const`用法的详细总结: 1. **const修饰普通变量和指针** 当`const`修饰普通变量时,变量的值...

    C++ const应用总结

    在C++编程语言中,`const`关键字是一个非常重要的特性,它用于定义常量和声明不可修改的对象。本文将深入探讨`const`在C++中的各种应用,帮助你更好地理解和运用这一关键概念。 1. 常量声明: `const`关键字用于...

    c++中const 的各种用法

    ### C++中const的各种用法详解 #### 一、引言 `const`关键字在C++编程语言中扮演着至关重要的角色。它不仅能够帮助程序员更精确地控制代码的行为,还可以提高程序的安全性和效率。本文将详细介绍`const`在C++中的...

    vs code C/C++语法高亮配置文件(C/C++ Themes插件)

    vscode配色插件的c/c++语法高亮配置文件,主题插件为C/C++ Themes。 可以对诸如const、enum、typedef别名、结构体引用等语法高亮进行设置,语言本身的关键字自然不用说了,比one dark pro等热门的插件颜色丰富的多。...

    林锐-高质量c/c++编程指南

    ### 林锐-高质量C/C++编程指南 #### 概述 《林锐-高质量C/C++编程指南》是一份由林锐博士编撰的专业文档,旨在为C/C++开发者提供一套规范化的编程指导原则。这份文档对于希望提升编程技能、编写出高质量代码的C++...

    实现trie树的C/C++模板

    ### 实现Trie树的C/C++模板 在计算机科学领域,Trie树(又称前缀树或字典树)是一种用于存储具有共同前缀的字符串的高效数据结构。它广泛应用于各种场景,如自动补全、拼写检查以及IP路由表等。本文将详细介绍如何...

    c++里const用法归纳

    C++中const用法归纳 C++中的const关键字是一种非常灵活的语言特性,它可以用来修饰变量、指针、函数参数、返回值、成员函数等多种类型。正确使用const关键字可以大大改善程序的健壮性和可读性。 1. Const常量 ...

    c/c++语言中const的用法,const 定义变量,指针的用法和不同

    C/C++ 语言中 const 的用法详解 在 C/C++ 语言中,const 关键字是一个非常重要的概念,它用于定义只读变量和常量。但是,const 的用法有一些细微的差别,容易让人感到困惑。在本文中,我们将详细介绍 C/C++ 语言中 ...

    C++ 中const总结

    ### C++ 中const总结 #### 一、const变量的基本概念 在C++中,`const`关键字用于声明常量或指定对象的一部分不可更改。例如,`const int bufSize = 512;` 这样的声明意味着`bufSize`是一个整型常量,其值在初始化...

    C++中const使用说明

    在C++编程语言中,`const`关键字是一个非常重要的特性,它用来定义不可修改的变量。`const`的使用能够帮助我们确保代码的稳定性和安全性,同时也能提高编译器的错误检查能力。以下是对`const`在C++中使用的详细说明...

    C++中const关键字详解

    ### C++中const关键字详解 #### 一、引言 在C++编程语言中,`const`关键字是一个非常重要的概念,它被广泛用于各种上下文中,以提高代码的安全性、可读性和效率。本文将详细介绍`const`关键字的用法及其在不同场景...

    Numerical Recipes in cpp for Financial

    date(const int& d, const int& m, const int& y); bool valid(void) const; int day() const; int month() const; int year() const; void set_day (const int& day ); void set_month (const int& month...

    sassify-object-and-prepend

    import sassifyObjectAndPrepend from 'sassify-object-and-prepend' ; // Input file path to be read from. // Example Sass input file might look like: /* .whatever { color: $blue; } */ const inputFilePath...

    c++中const用法详解

    ### C++中`const`关键字的详尽解析与应用实例 #### 一、`const`基本用法 **1.1 限定符声明变量只能被读** `const`关键字用于声明一个只读变量,这意味着一旦该变量被初始化后,其值就不能被更改。例如: ```cpp ...

Global site tag (gtag.js) - Google Analytics