I am building up a CRTP interface and noticed some undefined behavior. So, I built up some sample code to narrow down the problem. #include &lt;iostream&gt;  template &lt;typename T&gt; class Base { public:     int a() const { return static_cast&lt;T const&amp;&gt;(*this).a_IMPL(); }     int b() const { return static_cast&lt;T const&amp;&gt;(*this).b_IMPL(); }     int c() const { return static_cast&lt;T const&amp;&gt;(*this).c_IMPL(); } };  class A : public Base&lt;A&gt; { public:     A(int a, int b, int c) : _a(a), _b(b), _c(c) {}      int a_IMPL() const { return _a; }     int b_IMPL() const { return _b; }     int c_IMPL() const { return _c; }      private:     int _a;     int _b;     int _c; };  template &lt;typename T&gt; void foo(const T&amp; v) {     std::cout &lt;&lt; &quot;foo()&quot; &lt;&lt; std::endl;          std::cout &lt;&lt; &quot;a() = &quot; &lt;&lt; static_cast&lt;Base&lt;T&gt;&gt;(v).a() &lt;&lt; std::endl;     std::cout &lt;&lt; &quot;b() = &quot; &lt;&lt; static_cast&lt;Base&lt;T&gt;&gt;(v).b() &lt;&lt; std::endl;     std::cout &lt;&lt; &quot;c() = &quot; &lt;&lt; static_cast&lt;Base&lt;T&gt;&gt;(v).c() &lt;&lt; std::endl; }  int main() {     A v(10, 20, 30);          std::cout &lt;&lt; &quot;a() = &quot; &lt;&lt; v.a() &lt;&lt; std::endl;     std::cout &lt;&lt; &quot;b() = &quot; &lt;&lt; v.b() &lt;&lt; std::endl;     std::cout &lt;&lt; &quot;c() = &quot; &lt;&lt; v.c() &lt;&lt; std::endl;          foo(v);          return 0; }  The output of this code is: a() = 10 b() = 20 c() = 30 foo() a() = 134217855 b() = 0 c() = -917692416  It appears that there is some problem when casting the child class, which implements the CRTP &quot;interface&quot;, to the interface itself. This doesn't make sense to me because the class A plainly inherits from Base so, shouldn't I be able to cast an instance of A into Base? Thanks!
664
Someone mentioned it in the IRC as the slicing problem.
124139
I have a class which has many small functions. By small functions, I mean functions that doesn't do any processing but just return a literal value. Something like:  string Foo::method() const{     return "A"; }   I have created a header file "Foo.h" and source file "Foo.cpp". But since the function is very small, I am thinking about putting it in the header file itself. I have the following questions:   Is there any performance or other issues if I put these function definition in header file? I will have many functions like this. My understanding is when the compilation is done, compiler will expand the header file and place it where it is included. Is that correct?
36697
I was wondering if there is an iterator in the STL that dereferences the object pointed before returning it. This could be very useful when manipulating containers aggregating pointers. Here's an example of what I would like to be able to do:  #include &lt;vector&gt; #include &lt;iterator&gt; #include &lt;algorithm&gt;  using namespace std;  int main() {   vector&lt;int*&gt; vec;    int i = 1;   int j = 2;   int k = 3;    vec.push_back(&amp;i);   vec.push_back(&amp;j);   vec.push_back(&amp;k);    copy(deref_iterator(vec.begin()),         deref_iterator(vec.end()),         ostream_iterator&lt;int&gt;(cout, " ")); // prints "1 2 3"    return 0; }
48778
I am little confused with the applicability of reinterpret_cast vs static_cast. From what I have read the general rules are to use static cast when the types can be interpreted at compile time hence the word static. This is the cast the C++ compiler uses internally for implicit casts also.  reinterpret_casts are applicable in two scenarios:   convert integer types to pointer types and vice versa  convert one pointer type to another. The general idea I get is this is unportable and should be avoided.     Where I am a little confused is one usage which I need, I am calling C++ from C and the C code needs to hold on to the C++ object so basically it holds a void*. What cast should be used to convert between the void * and the Class type?  I have seen usage of both static_cast and reinterpret_cast? Though from what I have been reading it appears static is better as the cast can happen at compile time? Though it says to use reinterpret_cast to convert from one pointer type to another?
32521
I came across this strange code snippet which compiles fine:  class Car {     public:     int speed; };  int main() {     int Car::*pSpeed = &amp;Car::speed;     return 0; }   Why does C++ have this pointer to a non-static data member of a class? What is the use of this strange pointer in real code?
31381
The following code says that passing the map as const into the operator[] method discards qualifiers: #include &lt;iostream&gt; #include &lt;map&gt; #include &lt;string&gt;  using namespace std;  class MapWrapper { public:     const int &amp;get_value(const int &amp;key) const {         return _map[key];     }  private:     map&lt;int, int&gt; _map; };  int main() {     MapWrapper mw;     cout &lt;&lt; mw.get_value(42) &lt;&lt; endl;     return 0; }  Is this because of the possible allocation that occurs on the map access? Can no functions with map accesses be declared const? MapWrapper.cpp:10: error: passing const std::map&lt;int, int, std::less&lt;int&gt;, std::allocator&lt;std::pair&lt;const int, int&gt; &gt; &gt; as this argument of  _Tp&amp; std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::operator[](const _Key&amp;)  [with _Key = int, _Tp = int, _Compare = std::less&lt;int&gt;,  _Alloc = std::allocator&lt;std::pair&lt;const int, int&gt; &gt;] discards qualifiers
118197
In C++, is the return type considered part of the function signature? and no overloading is allowed with just return type modified.
65547
See also      Not a major issue, just annoying as I don't want my class to ever be instantiated without the particular arguments.  #include &lt;map&gt;  struct MyClass {     MyClass(int t); };  int main() {     std::map&lt;int, MyClass&gt; myMap;     myMap[14] = MyClass(42); }   This gives me the following g++ error:     /usr/include/c++/4.3/bits/stl_map.h:419: error: no matching function for call to ‘MyClass()’   This compiles fine if I add a default constructor; I am certain it's not caused by incorrect syntax.
33741
Compiler error  indicates that only the basic POD types (as well as strings, enums, and null references) can be declared as const. Does anyone have a theory on the rationale for this limitation? For instance, it would be nice to be able to declare const values of other types, such as IntPtr.  I believe that the concept of const is actually syntactic sugar in C#, and that it just replaces any uses of the name with the literal value. For instance, given the following declaration, any reference to Foo would be replaced with "foo" at compile time.  const string Foo = "foo";   This would rule out any mutable types, so maybe they chose this limitation rather than having to determine at compile time whether a given type is mutable?
59937
I am looking for a way to identify primitives types in a template class definition.  I mean, having this class :  template&lt;class T&gt; class A{ void doWork(){    if(T isPrimitiveType())      doSomething();    else      doSomethingElse();  } private: T *t;  };   Is there is any way to "implement" isPrimitiveType().
62585
Given the following example, why do I have to explicitly use the statement b-&gt;A::DoSomething() rather than just b-&gt;DoSomething()?  Shouldn't the compiler's overload resolution figure out which method I'm talking about?  I'm using Microsoft VS 2005. (Note: using virtual doesn't help in this case.)  class A {   public:     int DoSomething() {return 0;}; };  class B : public A {   public:     int DoSomething(int x) {return 1;}; };  int main() {   B* b = new B();   b-&gt;A::DoSomething();    //Why this?   //b-&gt;DoSomething();    //Why not this? (Gives compiler error.)   delete b;   return 0; }
107586
Can a class overload methods that also exist in the publicly inherited interface? It seems like this is unambiguous and useful, but compilers (VC, Intel, GCC) all complain, at least by my construction. Below is a toy example. The inherited rebound() function has two clear overloads, yet this will not compile.  If you rename the rebound() method in either class, it works fine, but if they share the same member function name (even though they're overloaded with different argument types!) you get a fatal error of "too few arguments to function call."  The workaround is trivial (I'll just rename the methods) but I'm just trying to understand if this is a C++ restriction (and why it would be).   #include  class Bound { public:   Bound() : x0(0.0), x1(0.0) {};   Bound(double x) : x0(x), x1(x) {};   double width() const {return x1-x0;}   void rebound(const Bound *a, const Bound *b); private:   double x0, x1; };  void Bound::rebound(const Bound *a, const Bound *b) {   if (a && b) {     x0=std::min(a->x0, b->x0);     x1=std::max(a->x1, b->x1);   } }  class Node : public Bound { public:   Node(double x) : Bound(x), left(0), right(0) {};   Node(Node *a, Node *b) : left(a), right(b) {rebound();}   void rebound() { rebound(left, right); } private:   Node *left;   Node *right; };   int main() {   Node A(1.0);   Node B(2.0);   Node C(&A, &B); }
42167
The following code:  template &lt;typename S, typename T&gt; struct foo {    void bar(); };  template &lt;typename T&gt; void foo &lt;int, T&gt;::bar() { }   gives me the error  invalid use of incomplete type 'struct foo&lt;int, T&gt;' declaration of 'struct foo&lt;int, T&gt;'   (I'm using gcc.) Is my syntax for partial specialization wrong? Note that if I remove the second argument:  template &lt;typename S&gt; struct foo {    void bar(); };  template &lt;&gt; void foo &lt;int&gt;::bar() { }   then it compiles correctly.
41728
A question related to :  What cast syntax style do you prefer in C++?   C-style cast syntax: (int)foo C++-style cast syntax: static_cast&lt;int&gt;(foo) constructor syntax: int(foo)   They may not translate to exactly the same instructions (do they?) but their effect should be the same (right?).  If you're just casting between the built-in numeric types, I find C++-style cast syntax too verbose. As a former Java coder I tend to use C-style cast syntax instead, but my local C++ guru insists on using constructor syntax.  What do you think?
52593
Yesterday, I found myself writing code like this:  SomeStruct getSomeStruct() {     SomeStruct input;      cin &gt;&gt; input.x;     cin &gt;&gt; input.y; }   Of course forgetting to actually return the struct I just created.  Oddly enough, the values in the struct that was returned by this function got initialized to zero (when compiled using g++ that is).  Is this just a coincidence or did another SomeStruct get created and initialized somewhere implicitly?
18652
I have the following class in C++:  class a {     const int b[2];     // other stuff follows      // and here's the constructor     a(void); }   The question is, how do I initialize b in the initialization list, given that I can't initialize it inside the body of the function of the constructor, because b is const?  This doesn't work:  a::a(void) :      b([2,3]) {      // other initialization stuff }   Edit:  The case in point is when I can have different values for b for different instances, but the values are known to be constant for the lifetime of the instance.
47351
Here is a little test program:   #include &lt;iostream&gt;  class Test { public:     static void DoCrash(){ std::cout&lt;&lt; "TEST IT!"&lt;&lt; std::endl; } };  int main() {     Test k;     k.DoCrash(); // calling a static method like a member method...      std::system("pause");      return 0; }   On VS2008 + SP1 (vc9) it compiles fine: the console just display "TEST IT!".  As far as I know, static member methods shouldn't be called on instanced object.   Am I wrong? Is this code correct from the standard point of view? If it's correct, why is that? I can't find why it would be allowed, or maybe it's to help using "static or not" method in templates?