const

STL Static-Const Member Definitions

How does the following work? #include <limits> int main() { const int* const foo = &std::numeric_limits<int> ::digits; } I was under the impression that in order to take an address of a static const-ant member we had to physically define it in some translation unit in order to please the linker. That said, after looking at the prep...

A reference that is not to 'const' cannot be bound to a non-lvalue

Hello, Am struggling a bit with this. Am declaring: BYTE *pImage = NULL; Used in call: m_pMyInterface->GetImage(i, &imageSize, &pImage); Visual C++ 2003 compiler error: error C2664: 'CJrvdInterface::GetImage' : cannot convert parameter 3 from 'BYTE **__w64 ' to 'BYTE **& ' A reference that is not to 'const' cannot be boun...

Why isn't the compiler smarter in this const function overloading problem?

The following code does not compile: #include <iostream> class Foo { std::string s; public: const std::string& GetString() const { return s; } std::string* GetString() { return &s; } }; int main(int argc, char** argv){ Foo foo; const std::string& s = foo.GetString(); // error return 0; } I get the following error: const...

Copy constructor, why in return by value functions

Suppose i have: class A { A(A& foo){ ..... } A& operator=(const A& p) { } } ... A lol; ... A wow(...) { return lol; } ... ... A stick; stick = wow(...); Then I'll get a compile error in the last line. But if I add 'const' before 'A&', its ok. I want to know why. Where it's exactly the problem? I dont get why it shoul...

How do I define constant values of UIColor?

I want to do something like this, but I cannot get a cooperative syntax. static const UIColor *colorNavbar = [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0]; I suppose that I could define macros, but they are ugly. ...

String concatenation in C

if I want to construct a const char * out of several primitive type arguments, is there a way to build the string using a similar to the printf? ...

How to initialize const float32x4x4_t (ARM NEON intrinsic, GCC) ?

I can initialize float32x4_t like this: const float32x4x4_t zero = { 0.0f, 0.0f, 0.0f, 0.0f }; But this code makes an error Incompatible types in initializer: const float32x4x4_t one = { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, }; float32x4x4_t is 4x4 matrix bui...

Why can't I pass const map structure to a function in c++?

I tried to pass const with vector it works: Ex: void damn(const vector <bool> &bb) { for (int i=0; i<bb.size(); i++) cout<<bb[i]<<endl; } But when trying with map, it does not: void pas(const map <string, float> &mm) { cout<<mm["a"]; cout<<mm["b"]; } I wonder why it doesn't. ...

Embarassing C++ question regarding const

My comments on this answer got me thinking about the issues of constness and sorting. I played around a bit and reduced my issues to the fact that this code: #include <vector> int main() { std::vector <const int> v; } will not compile - you can't create a vector of const ints. Obviously, I should have known this (and intellect...

Why is passing a string literal into a char* argument only sometimes a compiler error?

I'm working in a C, and C++ program. We used to be compiling without the make-strings-writable option. But that was getting a bunch of warnings, so I turned it off. Then I got a whole bunch of errors of the form "Cannot convert const char* to char* in argmuent 3 of function foo". So, I went through and made a whole lot of changes to fix...

Make All Types Constant by Default in C++

What is the simplest and least obtrusive way to indicate to the compiler, whether by means of compiler options, #defines, typedefs, or templates, that every time I say T, I really mean T const? I would prefer not to make use of an external preprocessor. Since I don't use the mutable keyword, that would be acceptable to repurpose to indic...

Why is std::numeric_limits<T>::max() a function?

In the standard library of C++ the value std::numeric_limits<T>::max() is specified as a function. Further properties of a specific type are given as constants (like std::numeric_limits<T>::is_signed). All constants that are of type T are given as functions, whereas all other constants are given as, well, constant values. Whats the rati...

Can I use string concatenation to define a class CONST in PHP?

I know that you can create global constants in terms of each other using string concatenation: define('FOO', 'foo'); define('BAR', FOO.'bar'); echo BAR; will print 'foobar'. However, I'm getting an error trying to do the same using class constants. class foobar { const foo = 'foo'; const foo2 = self::foo; const bar = self::f...

Why can't I access a const vector with iterator?

My example is as below. I found out the problem is with "const" in function void test's parameter. I don't know why the compiler does not allow. Could anybody tell me? Thanks. vector<int> p; void test(const vector<int> &blah) { vector<int>::iterator it; for (it=blah.begin(); it!=blah.end(); it++) { cout<<*it<<" "; } }...

How to iterate properly across a const set?

I'm working on a program that's supposed to represent a graph. My issue is in my printAdjacencyList function. Basically, I have a Graph ADT that has a member variable "nodes", which is a map of the nodes of that graph. Each Node has a set of Edge* to the edges it is connected to. I'm trying to iterate across each node in the graph and ea...

pure/const functions in C++

Hi, I'm thinking of using pure/const functions more heavily in my C++ code. (pure/const attribute in GCC) However, I am curious how strict I should be about it and what could possibly break. The most obvious case are debug outputs (in whatever form, could be on cout, in some file or in some custom debug class). I probably will have a ...

pure/const function attributes in different compilers

pure is a function attribute which says that a function does not modify any global memory. const is a function attribute which says that a function does not read/modify any global memory. Given that information, the compiler can do some additional optimisations. Example for GCC: float sigmoid(float x) __attribute__ ((const)); float c...

Namespace constant in C#

Is there any way to define a constant for an entire namespace, rather than just within a class? For example: namespace MyNamespace { public const string MY_CONST = "Test"; static class Program { } } Gives a compile error as follows: Expected class, delegate, enum, interface, or struct ...

C++: combine const with template arguments

The following example is working when I manualy replace T wirh char *, but why is not working as it is: template <typename T> class A{ public: A(const T _t) { } }; int main(){ const char * c = "asdf"; A<char *> a(c); } When compiling with gcc, I get this error: test.cpp: In function 'int main()': test.cpp:10: error: invali...

Assigning a pointer variable to a const int in C++?

I'm wondering if anyone can explain the following to me: If I write int i = 0; float* pf = i; I get a compile error (gcc 4.2.1): error: invalid conversion from ‘int’ to ‘float*’ Makes sense - they are obviously two completely different types. But if instead I write const int i = 0; float* pf = i; It compiles without error. Wh...