Used in a Sentence

const

Definitions, parts of speech, synonyms, and sentence examples for const.

Editorial note

So maybe the problem is people declaring parameters as T & or T *, when they meant const T & or const T *?

Examples17
Definitions3
Parts of speech2

Quick take

(programming) A variable whose value cannot be changed directly.

Meaning at a glance

The clearest senses and uses of const gathered in one view.

noun

(programming) A variable whose value cannot be changed directly.

adjective

(programming, of a variable) Whose value cannot be changed directly.

adjective

(programming, of a function) Which cannot change the value of its class’s attributes.

Definitions

Core meanings and parts of speech for const.

noun

(programming) A variable whose value cannot be changed directly.

adjective

(programming, of a variable) Whose value cannot be changed directly.

adjective

(programming, of a function) Which cannot change the value of its class’s attributes.

Example sentences

1

So maybe the problem is people declaring parameters as T & or T *, when they meant const T & or const T *?

2

Compare that to: Iterator find(T& value) const; This function signature accepts only non-const references (unless T is const).

3

Because what you're describing sounds like it's going to be a hot mess, const or no const.

4

The function takes const T & or const T *, because it doesn't change that argument.

5

If you're writing a library, the API should avoid including container details in function parameters, and const and non-const T should be allowed.

6

A non-const argument will be implicitly cast to const by the compiler.

7

So you can have T * (variable RAM address), T * const (fixed RAM address), const T * (variable ROM/RAM address), and const T * const (fixed ROM/RAM address).

8

The most common usage of const in library code is for function parameters: Iterator find(T const& value) const; The const actually _helps_ callers: find accepts both const and non-const references.

9

You passed what's supposedly a const object into a function that expected to be able to change it.

10

> but in C-family languages, 'const' modifies type, making objects that benefit from the compiler hints 'const' implies type-incompatible with objects that do not.

11

So, say you have a class with a const member, and you have to pass it in to a function?

12

But the underlying problem sounds like: you have a const object, and you want to pass it into a function.

Quote examples

1

As I recall isn't there also "const T const &"?

2

The equivalent in C++ would be: SomeClass * const foo = &instanceOfSomeClass; Here, the pointer "foo" could not change, but operations changing foo would be allowed.

3

Whenever I need to return a non-const reference/pointer to some inner data (which is not often) I name the method "mutableFoo()" to make clear what you're getting.

4

(String literals are const char *.) void blah(vector<const char * > &xs) { xs.push_back("blah"); } And you have the code calling it: vector<char * > xs; blah(xs); And that would not be valid.

Proper noun examples

1

Suppose we represented Add instead as: case class Add(exprs: NonEmptyList[Expr with NotAdd]) extends Expr case class Const(value: Int) extends Expr with NotAdd case class...

Frequently asked questions

Short answers drawn from the clearest meanings and examples for this word.

How do you use const in a sentence?

So maybe the problem is people declaring parameters as T & or T *, when they meant const T & or const T *?

What does const mean?

(programming) A variable whose value cannot be changed directly.

What part of speech is const?

const is commonly used as noun, adjective.