I have a line that looks as follows:  F::enable&lt;sizeof(value_type), offset&gt;(index);   I get the following compiler error (GCC 6.3.1):  error: invalid operands of types ‘&lt;unresolved overloaded function type&gt;’ and ‘long unsigned int’ to binary ‘operator&lt;’   Obviously, the left angle bracket is being interpreted as the less than operator, instead of the start of a template parameter list. F is a template parameter (class F) which I expect to contain a static member function with the signature:  template &lt;GLsizei stride, const GLvoid* offset&gt; static void enable(GLuint index);   I've tried liberally inserting whitespaces, looked into typedeffing the function, looked into moving to a regular function (not possible due to inheritance things). I have commented out the line to see if something above was causing the problem, no dice. Is there any way of explaining to the compiler that I don't want to compare a function and a size_t, I want to call a particular specialization of static member function in a class?
211
In templates, where and why do I have to put typename and template on dependent names? What exactly are dependent names anyway?    I have the following code:  template &lt;typename T, typename Tail&gt; // Tail will be a UnionNode too. struct UnionNode : public Tail {     // ...     template&lt;typename U&gt; struct inUnion {         // Q: where to add typename/template here?         typedef Tail::inUnion&lt;U&gt; dummy;      };     template&lt; &gt; struct inUnion&lt;T&gt; {     }; }; template &lt;typename T&gt; // For the last node Tn. struct UnionNode&lt;T, void&gt; {     // ...     template&lt;typename U&gt; struct inUnion {         char fail[ -2 + (sizeof(U)%2) ]; // Cannot be instantiated for any U     };     template&lt; &gt; struct inUnion&lt;T&gt; {     }; };   The problem I have is in the typedef Tail::inUnion&lt;U&gt; dummy line. I'm fairly certain that inUnion is a dependent name, and VC++ is quite right in choking on it. I also know that I should be able to add template somewhere to tell the compiler that inUnion is a template-id. But where exactly? And should it then assume that inUnion is a class template, i.e. inUnion&lt;U&gt; names a type and not a function?
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?
30298
In C++, what is the purpose of the scope resolution operator when used without a scope? For instance:  ::foo();
43864
The following code snippet (correctly) gives a warning in C and an error in C++ (using gcc &amp; g++ respectively, tested with versions 3.4.5 and 4.2.1; MSVC does not seem to care):  char **a; const char** b = a;   I can understand and accept this. The C++ solution to this problem is to change b to be a const char * const *, which disallows reassignment of the pointers and prevents you from circumventing const-correctness ().  char **a; const char* const* b = a;   However, in pure C, the corrected version (using const char * const *) still gives a warning, and I don't understand why. Is there a way to get around this without using a cast?  To clarify: 1) Why does this generate a warning in C?  It should be entirely const-safe, and the C++ compiler seems to recognize it as such. 2) What is the correct way to go about accepting this char** as a parameter while saying (and having the compiler enforce) that I will not be modifying the characters it points to? For example, if I wanted to write a function:  void f(const char* const* in) {   // Only reads the data from in, does not write to it }   And I wanted to invoke it on a char**, what would be the correct type for the parameter?
71767
I could be totally wrong here, but as I understand it, C++ doesn't really have a native "pointer to member function" type.  I know you can do tricks with Boost and mem_fun etc. But why did the designers of C++ decide not to have a 64-bit pointer containing a pointer to the function and a pointer to the object, for example?  What I mean specifically is a pointer to a member function of a particular object of unknown type.  I.E. something you can use for a callback.  This would be a type which contains two values. The first value being a pointer to the function, and the second value being a pointer to the specific instance of the object.  What I do not mean is a pointer to a general member function of a class. E.G.  int (Fred::*)(char,float)   It would have been so useful and made my life easier.  Hugo
23755
Generally, when using the conditional operator, here's the syntax:  int x = 6; int y = x == 6 ? 5 : 9;   Nothing fancy, pretty straight forward.  Now, let's try to use this when assigning a Lambda to a Func type. Let me explain:  Func&lt;Order, bool&gt; predicate = id == null     ? p =&gt; p.EmployeeID == null     : p =&gt; p.EmployeeID == id;   That's the same syntax, and should work? Right? For some reason that doesn't. The compiler gives this nice cryptic message:     Error  1   Type of conditional expression cannot be determined because there is no implicit conversion between 'lambda expression' and 'lambda expression'   I then went ahead and changed the syntax and this way it did work:  Func&lt;Order, bool&gt; predicate = id == null     ? predicate = p =&gt; p.EmployeeID == null     : predicate = p =&gt; p.EmployeeID == id;   I'm just curious as to why it doesn't work the first way?  (Side note: I ended up not needing this code, as I found out that when comparing an int value against null, you just use object.Equals)
222
In several C++ examples I see a use of the type size_t where I would have used a simple int. What's the difference, and why size_t should be better?
29551
I've heard that the static_cast function should be preferred to C-style or simple function-style casting.  Is this true?  Why?
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
50427
We all know that you can overload a function according to the parameters:  int mul(int i, int j) { return i*j; } std::string mul(char c, int n) { return std::string(n, c); }    Can you overload a function according to the return value? Define a function that returns different things according to how the return value is used:  int n = mul(6, 3); // n = 18 std::string s = mul(6, 3); // s = "666" // Note that both invocations take the exact same parameters (same types)   You can assume the first parameter is between 0-9, no need to verify the input or have any error handling.
19841
I've been programming in C++ for a few years, and I've used STL quite a bit and have created my own template classes a few times to see how it's done.  Now I'm trying to integrate templates deeper into my OO design, and a nagging thought keeps coming back to me: They're just a macros, really... You could implement (rather UGLY) auto_ptrs using #defines, if you really wanted to.  This way of thinking about templates helps me understand how my code will actually work, but I feel that I must be missing the point somehow. Macros are meant evil incarnate, yet "template metaprogramming" is all the rage.  So, what ARE the real distinctions? and how can templates avoid the dangers that #define leads you into, like   Inscrutable compiler errors in places where you don't expect them? Code bloat?  Difficulty in tracing code? Setting Debugger Breakpoints?
45776
I'm trying to do something like the following:  enum E;  void Foo(E e);  enum E {A, B, C};   which the compiler rejects.  I've had a quick look on Google and the consensus seems to be "you can't do it", but I can't understand why.  Can anyone explain?  Clarification 2: I'm doing this as I have private methods in a class that take said enum, and I do not want the enum's values exposed - so, for example, I do not want anyone to know that E is defined as  enum E {     FUNCTIONALITY_NORMAL, FUNCTIONALITY_RESTRICTED, FUNCTIONALITY_FOR_PROJECT_X }   as project X is not something I want my users to know about.  So, I wanted to forward declare the enum so I could put the private methods in the header file, declare the enum internally in the cpp, and distribute the built library file and header to people.  As for the compiler - it's GCC.
27874
In C++, is there any difference between:  struct Foo { ... };   and:  typedef struct { ... } Foo;
2552
Possible Duplicate:          I've coded something like this:  public bool IsDataChanged() {                T value1 = GetValue2;     T value2 = GetValue1();      return (valueInDB != valueFromView); }   Right now the function doesn't compile with the error "Operator '!=' cannot be applied to operands of type 'T' and 'T'". What do I have to do to make this function work ?
15076
I have code like this:  class RetInterface {...}  class Ret1: public RetInterface {...}  class AInterface {   public:      virtual boost::shared_ptr&lt;RetInterface&gt; get_r() const = 0;      ... };  class A1: public AInterface {   public:      boost::shared_ptr&lt;Ret1&gt; get_r() const {...}      ... };   This code does not compile.  In visual studio it raises     C2555: overriding virtual function return type differs and is not   covariant   If I do not use boost::shared_ptr but return raw pointers, the code compiles (I understand this is due to  in C++). I can see the problem is because boost::shared_ptr of Ret1 is not derived from boost::shared_ptr of RetInterface. But I want to return boost::shared_ptr of Ret1 for use in other classes, else I must cast the returned value after the return.      Am I doing something wrong?  If not, why is the language like this - it should be extensible to handle conversion between smart pointers in this scenario? Is there a desirable workaround?
44161
When I write C++ code for a class using templates and split the code between a source (CPP) file and a header (H) file, I get a whole lot of "unresolved external symbol" errors when it comes to linking the final executible, despite the object file being correctly built and included in the linking.  What's happening here, and how can I fix it?
46741
In some code I've inherited, I see frequent use of size_t with the std namespace qualifier.  For example:  std::size_t n = sizeof( long );   It compiles and runs fine, of course.  But it seems like bad practice to me (perhaps carried over from C?).  Isn't it true that size_t is built into C++ and therefore in the global namespace?  Is a header file include needed to use size_t in C++?  Another way to ask this question is, would the following program (with no includes) be expected to compile on all C++ compilers?  size_t foo() {     return sizeof( long ); }