- 浏览: 400913 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
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]) { // ... }
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15345首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1019RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 773It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 972As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1065typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 781There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 830we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 992As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 755There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 961Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 981As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 1000As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1334in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 944class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 991template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1213There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 860the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 593The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1173In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 877A class member operator new() c ...
相关推荐
C++语言中的const关键字是一个非常重要的修饰符,它的核心作用是声明一个变量为常量,即不可修改。正确地理解和使用const,可以帮助提高程序的健壮性和安全性。本文将详细介绍const关键字在C++中的使用方法。 一、...
C++ 中 const 用法全解 const 在 C++ 中占有重要作用,属于小兵立大功的典型,本文档详细介绍了如何使用 const。 1. const 常量 在 C++ 中,const 可以用来修饰变量,称为常量。例如:const int max = 100; 这种...
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++中const用法总结 #### 一、引言 `const` 是 C++ 中一个非常重要的关键字,用于声明常量或指定某些实体不可被修改。熟练掌握 `const` 的使用方法对于编写高质量的 C++ 代码至关重要。本文将详细介绍 `const`...
vscode配色插件的c/c++语法高亮配置文件,主题插件为C/C++ Themes。 可以对诸如const、enum、typedef别名、结构体引用等语法高亮进行设置,语言本身的关键字自然不用说了,比one dark pro等热门的插件颜色丰富的多。...
在C++编程语言中,`const`关键字是一个非常重要的元素,用于声明不可变性,确保程序的稳定性和安全性。以下是对C++中`const`用法的详细总结: 1. **const修饰普通变量和指针** 当`const`修饰普通变量时,变量的值...
在C++编程语言中,`const`关键字是一个非常重要的特性,它用于定义常量和声明不可修改的对象。本文将深入探讨`const`在C++中的各种应用,帮助你更好地理解和运用这一关键概念。 1. 常量声明: `const`关键字用于...
### C++中const的各种用法详解 #### 一、引言 `const`关键字在C++编程语言中扮演着至关重要的角色。它不仅能够帮助程序员更精确地控制代码的行为,还可以提高程序的安全性和效率。本文将详细介绍`const`在C++中的...
### 林锐-高质量C/C++编程指南 #### 概述 《林锐-高质量C/C++编程指南》是一份由林锐博士编撰的专业文档,旨在为C/C++开发者提供一套规范化的编程指导原则。这份文档对于希望提升编程技能、编写出高质量代码的C++...
### 实现Trie树的C/C++模板 在计算机科学领域,Trie树(又称前缀树或字典树)是一种用于存储具有共同前缀的字符串的高效数据结构。它广泛应用于各种场景,如自动补全、拼写检查以及IP路由表等。本文将详细介绍如何...
C++中const用法归纳 C++中的const关键字是一种非常灵活的语言特性,它可以用来修饰变量、指针、函数参数、返回值、成员函数等多种类型。正确使用const关键字可以大大改善程序的健壮性和可读性。 1. Const常量 ...
C/C++ 语言中 const 的用法详解 在 C/C++ 语言中,const 关键字是一个非常重要的概念,它用于定义只读变量和常量。但是,const 的用法有一些细微的差别,容易让人感到困惑。在本文中,我们将详细介绍 C/C++ 语言中 ...
### C++ 中const总结 #### 一、const变量的基本概念 在C++中,`const`关键字用于声明常量或指定对象的一部分不可更改。例如,`const int bufSize = 512;` 这样的声明意味着`bufSize`是一个整型常量,其值在初始化...
在C++编程语言中,`const`关键字是一个非常重要的特性,它用来定义不可修改的变量。`const`的使用能够帮助我们确保代码的稳定性和安全性,同时也能提高编译器的错误检查能力。以下是对`const`在C++中使用的详细说明...
### C++中const关键字详解 #### 一、引言 在C++编程语言中,`const`关键字是一个非常重要的概念,它被广泛用于各种上下文中,以提高代码的安全性、可读性和效率。本文将详细介绍`const`关键字的用法及其在不同场景...
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...
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`关键字的详尽解析与应用实例 #### 一、`const`基本用法 **1.1 限定符声明变量只能被读** `const`关键字用于声明一个只读变量,这意味着一旦该变量被初始化后,其值就不能被更改。例如: ```cpp ...
File Input/Output C++ File I/O Conversion Routines Binary and ASCII Files The End-of-Line Puzzle Binary I/O Buffering Problems Unbuffered I/O Designing File Formats C-Style I/O Routines C-Style ...