The following repo compiles fine:  #include &lt;tuple&gt; #include &lt;vector&gt;  class Foo { public:      typedef std::tuple&lt;int, int&gt; foo_type;      std::vector&lt;foo_type&gt;::iterator Find() {         return Listeners.begin();     }      std::vector&lt;foo_type&gt; Listeners; };  int main()  {     Foo foo;      auto i = foo.Find(); }   But when I template the tuple on P, Q as in the following repo, I get this error:     syntax error : missing ';' before identifier 'Find'   #include &lt;tuple&gt; #include &lt;vector&gt;  template&lt;typename P, typename Q&gt; class Foo { public:      typedef std::tuple&lt;P, Q&gt; foo_type;      std::vector&lt;foo_type&gt;::iterator Find() {         return Listeners.begin();     }      std::vector&lt;foo_type&gt; Listeners; };  int main() {     Foo&lt;int, int&gt; foo;      auto i = foo.Find(); }
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?
1488
Can anyone explain why following code won't compile? At least on g++ 4.2.4.  And more interesting, why it will compile when I cast MEMBER to int?  #include &lt;vector&gt;  class Foo {   public:       static const int MEMBER = 1;   };  int main(){       vector&lt;int&gt; v;       v.push_back( Foo::MEMBER );       // undefined reference to `Foo::MEMBER'     v.push_back( (int) Foo::MEMBER ); // OK       return 0; }
69483
I have code like this:  template &lt;typename T, typename U&gt; struct MyStruct {     T aType;     U anotherType; };  class IWantToBeFriendsWithMyStruct {     friend struct MyStruct; //what is the correct syntax here ? };   What is the correct syntax to give friendship to the template ?
65868
There's a good discussion of Generics and what they really do behind the scenes over at , so we all know that Vector&lt;int[]&gt; is a vector of integer arrays, and HashTable&lt;String, Person&gt; is a table of whose keys are strings and values Persons.  However, what stumps me is the usage of Class&lt;&gt;.  The java class Class is supposed to also take a template name, (or so I'm being told by the yellow underline in eclipse).  I don't understand what I should put in there.  The whole point of the Class object is when you don't fully have the information about an object, for reflection and such.  Why does it make me specify which class the Class object will hold?  I clearly don't know, or I wouldn't be using the Class object, I would use the specific one.
17104
Consider these classes.  class Base {    ... };  class Derived : public Base {    ... };   this function  void BaseFoo( std::vector&lt;Base*&gt;vec ) {     ... }   And finally my vector  std::vector&lt;Derived*&gt;derived;   I want to pass derived to function BaseFoo, but the compiler doesn't let me. How do I solve this, without copying the whole vector to a std::vector&lt;Base*&gt;?
93459
I have a dll that contains a templated class. Is there a way to export it without explicit specification?
55385
This is one of the possible ways I come out:  struct RetrieveKey {     template &lt;typename T&gt;     typename T::first_type operator()(T keyValuePair) const     {         return keyValuePair.first;     } };  map&lt;int, int&gt; m; vector&lt;int&gt; keys;  // Retrieve all keys transform(m.begin(), m.end(), back_inserter(keys), RetrieveKey());  // Dump all keys copy(keys.begin(), keys.end(), ostream_iterator&lt;int&gt;(cout, "\n"));   Of course, we can also retrieve all values from the map by defining another functor RetrieveValues.  Is there any other way to achieve this easily? (I'm always wondering why std::map does not include a member function for us to do so.)
39454
Why do we need both using namespace and include directives in C++ programs?  For example,   #include &lt;iostream&gt;  using namespace std;  int main() {  cout &lt;&lt; "Hello world"; }   Why is it not enough to just have #include &lt;iostream&gt; or just have using namespace std and get rid of the other?  (I am thinking of an analogy with Java, where import java.net.* will import everything from java.net, you don't need to do anything else.)
124250
I am in the process of learning Rust and am still having some issues with borrowing and ownership. I find myself trying to borrow mutable references that are already borrowed as immutable references etc...  I noticed that when I try to iterate over the primes without owning them, I encounter issues trying to modify the composites hash map. I was able to get around this by calling to_owned on the vector. Is this the right way to handle this?  I also see that when I build the code, I get warnings about unused code for warning: struct is never constructed: 'Sieve' and warning: method is never used: 'new', am I constructing them incorrectly?  use std::collections::HashMap;  struct Sieve {     composites: HashMap&lt;u64, Vec&lt;u64&gt;&gt;,     x: u64, }  impl Sieve {     fn new() -&gt; Sieve {         Sieve {             composites: HashMap::new(),             x: 2,         }     } }  impl Iterator for Sieve {     type Item = u64;      fn next(&amp;mut self) -&gt; Option&lt;u64&gt; {         let x = self.x;         self.x = self.x + 1;         match self.composites.get(&amp;x) {             Some(numbers) =&gt; {                 for _num in numbers.to_owned() {                     self.composites                         .entry(x + _num)                         .and_modify(|v| v.push(_num))                         .or_insert(vec![_num]);                 }                 self.composites.remove(&amp;x);                 self.next()             }             None =&gt; {                 self.composites.insert(x * x, vec![x]);                 Some(x)             }         }     } }  fn main() {     let mut sieve = Sieve::new();      println!("{:?}", sieve.next()); // 2     println!("{:?}", sieve.next()); // 3     println!("{:?}", sieve.next()); // 5     println!("{:?}", sieve.next()); // 7 }   .  I , this made it difficult to get feedback on. I have refactored the code to use iterators
67578
I haven't written any C++ in years and now I'm trying to get back into it. I then ran across this and thought about giving up:  typedef enum TokenType {     blah1   = 0x00000000,     blah2   = 0X01000000,     blah3   = 0X02000000 } TokenType;   What is this? Why is the typedef keyword used here? Why does the name TokenType appear twice in this declaration? How are the semantics different from this:  enum TokenType {     blah1 = 0x00000000,     blah2=0x01000000,     blah3=0x02000000 };
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?
60879
Can anyone recommend any good resources for learning C++ Templates?  Many thanks.
122587
I am trying to find a generic way of accessing a set of containers. I have a standard vector and list in addition to another custom list.  The custom list defines an iterator;  class Iterator: public std::iterator&lt;std::forward_iterator_tag, T&gt; {     // ... }  Iterator begin() {     return (Iterator(root)); }  Iterator end() {     return (Iterator(NULL)); }   with the appropriate operators overloaded.   Ideally, I would like to do this;  class Foo { public:     Foo() {         std::list&lt;int&gt; x;         std::vector&lt;int&gt; y;         custom_list&lt;int&gt; z;          iter = x.begin(); // OR         iter = y.begin(); // OR         iter = z.begin();          // ...     }; private:     std::iterator&lt;int&gt; iter; };   But obviously these are all iterators of different types. I can assume all the containers are of the same type however.  Is there an elegant way to solve this problem?
82685
I checked out a copy of a C++ application from SourceForge (HoboCopy, if you're curious) and tried to compile it.  Visual Studio tells me that it can't find a particular header file. I found the file in the source tree, but where do I need to put it, so that it will be found when compiling?    Are there special directories?
36051
class Test {        bool isVal() const {           return isVal;       }    private:        bool isVal; };   On Compiling this file it says      testClass.cpp:9: declaration of `bool   Test::isVal'      testClass.cpp:3: conflicts with   previous declaration `bool   Test::isVal()'   Although the same would work for java   class Test {    private boolean isVal;    public boolean isVal() {       return isVal;   }  }   Why does the compile error occur in C++ not in Java?
42083
What are the C++ coding and file organization guidelines you suggest for people who have to deal with lots of interdependent classes spread over several source and header files?  I have this situation in my project and solving class definition related errors crossing over several header files has become quite a headache.
24839
class Outer {      class Inner {     public:         Inner() {}         void func() ;     };  private:     static const char* const MYCONST;     int var; };  void Outer::Inner::func() {     var = 1; }  const char* const Outer::MYCONST = "myconst";   This errors out when I compile with class Outer::Inner' has no member named `var'