I have a functor which operates on a container of type U of elements of type T like so  template&lt;typename T, template&lt;typename...&gt; class U&gt; class asserter  { public:     asserter(U&lt;T&gt; &amp;c) : container(c) { };     void operator()(T lhs)      {         CU_ASSERT(container.find(lhs) != container.end());     }; private:     U&lt;T&gt;    &amp;container; };   which I might use as   std::set&lt;std::string&gt; a, c; ... asserter&lt;std::string, std::set&gt; ass(c); for_each(a.begin(), a.end(), ass);   Where we are ignoring std::includes() for the moment.   This works great if the container is one where U::find() is defined. If it's not I'd like to fall back to std::find(). On the other hand I'd rather use U::find() over std::find() if it's available.   In C++11 (or 17 if necessary) can I determine if U::find() is available (possibly restricting to the STL) for U and if so use it, otherwise use std::find()?
18976
Is it possible to write a template that changes behavior depending on if a certain member function is defined on a class?  Here's a simple example of what I would want to write:  template&lt;class T&gt; std::string optionalToString(T* obj) {     if (FUNCTION_EXISTS(T-&gt;toString))         return obj-&gt;toString();     else         return "toString not defined"; }   So, if class T has toString() defined, then it uses it; otherwise, it doesn't. The magical part that I don't know how to do is the "FUNCTION_EXISTS" part.
5777
I was looking at the API documentation for stl vector, and noticed there was no method on the vector class that allowed the removal of an element with a certain value. This seems like a common operation, and it seems odd that there's no built in way to do this.
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.)
1296
Why isn't  generic?    Seems like Collection&lt;E&gt; could have boolean remove(E o);   Then, when you accidentally try to remove (for example) Set&lt;String&gt; instead of each individual String from a Collection&lt;String&gt;, it would be a compile time error instead of a debugging problem later.
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.
110259
Ierator and for-each loop both we use for traverse no of items, I have used for-each many times but I am not clear about iterator, means when we should use ierator. I am studying salesforce workbook example..  global class CustomIterable implements iterator&lt;Account&gt;{    public List&lt;Account&gt; accs {get; set;}   public Integer i {get; set;}    public CustomIterable(){      accs =[SELECT Name fROM Account limit 5];      i = 0;              }   global boolean hasNext(){          if(i &gt;= accs.size()) {          return false;         } else {          return true;         }    }    global Account next(){          if(i == 8){return null;}          i++;          return accs[i-1];     } }   //------------  @isTest(seealldata=true) global class IteratorClass{     public static testmethod void main(){          IteratorClass obj = new IteratorClass();         Iterator&lt;Account&gt; acs =  obj.Iterator();         system.debug(acs);      }     global Iterator&lt;Account&gt; Iterator(){             return new CustomIterable();          }  }
50233
I have built myself a generic collection class which is defined like this.   public class StatisticItemHits&lt;T&gt;{...}   This class can be used with int and string values only. However this  public class StatisticItemHits&lt;T&gt; where T : string, int {...}   won't compile. What am I doing wrong?
78770
When authoring a library in a particular namespace, it's often convenient to provide overloaded operators for the classes in that namespace.  It seems (at least with g++) that the overloaded operators can be implemented either in the library's namespace:  namespace Lib { class A { };  A operator+(const A&amp;, const A&amp;); } // namespace Lib   or the global namespace  namespace Lib { class A { }; } // namespace Lib  Lib::A operator+(const Lib::A&amp;, const Lib::A&amp;);   From my testing, they both seem to work fine.  Is there any practical difference between these two options?  Is either approach better?
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?
80591
Although I'm doubtful, I'm curious as to whether it's possible to extract primitive-type template parameters from an existing type, perhaps using RTTI.  For example:  typedef std::bitset&lt;16&gt; WordSet;   Would it be possible to extract the number 16 in the above code without hard-coding it elsewhere? Compiler specific implementations are welcome, though I'm particularly interested in g++.
49609
I have a bunch of enum types in some library header files that I'm using, and I want to have a way of converting enum values to user strings - and vice-versa.   RTTI won't do it for me, because the 'user strings' need to be a bit more readable than the enumerations.  A brute force solution would be a bunch of functions like this, but I feel that's a bit too C-like.  enum MyEnum {VAL1, VAL2,VAL3};  String getStringFromEnum(MyEnum e) {   switch e   {   case VAL1: return "Value 1";   case VAL2: return "Value 2";   case VAL1: return "Value 3";   default: throw Exception("Bad MyEnum");   } }   I have a gut feeling that there's an elegant solution using templates, but I can't quite get my head round it yet.  UPDATE: Thanks for suggestions - I should have made clear that the enums are defined in a third-party library header, so I don't want to have to change the definition of them.  My gut feeling now is to avoid templates and do something like this:  char * MyGetValue(int v, char *tmp); // implementation is trivial  #define ENUM_MAP(type, strings) char * getStringValue(const type &amp;T) \  { \  return MyGetValue((int)T, strings); \  }  ; enum eee {AA,BB,CC}; - exists in library header file  ; enum fff {DD,GG,HH};   ENUM_MAP(eee,"AA|BB|CC") ENUM_MAP(fff,"DD|GG|HH")  // To use...      eee e;     fff f;     std::cout&lt;&lt; getStringValue(e);     std::cout&lt;&lt; getStringValue(f);
25742
I'm interested: What is C#'s analog of std::pair in C++? I found System.Web.UI.Pair class, but I'd prefer something template-based.  Thank you!
7208
Is it possible to forward declare an standard container in a header file?  For example, take the following code:  #include &lt;vector&gt;  class Foo { private:     std::vector&lt;int&gt; container_;     ... };   I want to be able to do something like this:  namespace std {     template &lt;typename T&gt; class vector; }  class Foo { private:     std::vector&lt;int&gt; container_;     ... };   Can this be done?
51146
Here is an example of polymorphism from  (edited for readability):  // abstract base class #include &lt;iostream&gt; using namespace std;  class Polygon {     protected:         int width;         int height;     public:         void set_values(int a, int b) { width = a; height = b; }         virtual int area(void) =0; };  class Rectangle: public Polygon {     public:         int area(void) { return width * height; } };  class Triangle: public Polygon {     public:         int area(void) { return width * height / 2; } };  int main () {     Rectangle rect;     Triangle trgl;     Polygon * ppoly1 = &amp;rect;     Polygon * ppoly2 = &amp;trgl;     ppoly1-&gt;set_values (4,5);     ppoly2-&gt;set_values (4,5);     cout &lt;&lt; ppoly1-&gt;area() &lt;&lt; endl; // outputs 20     cout &lt;&lt; ppoly2-&gt;area() &lt;&lt; endl; // outputs 10     return 0; }   My question is how does the compiler know that ppoly1 is a Rectangle and that ppoly2 is a Triangle, so that it can call the correct area() function? It could find that out by looking at the "Polygon * ppoly1 = &rect;" line and knowing that rect is a Rectangle, but that wouldn't work in all cases, would it? What if you did something like this?  cout &lt;&lt; ((Polygon *)0x12345678)-&gt;area() &lt;&lt; endl;   Assuming that you're allowed to access that random area of memory.  I would test this out but I can't on the computer I'm on at the moment.  (I hope I'm not missing something obvious...)
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; }
57217
Compiling the following code and got the error of type illegal.  int main() {     // Compilation error - switch expression of type illegal     switch(std::string("raj"))     {     case"sda":     } }   You cannot use string in either switch or case. Why? Is there any solution that works nicely to support logic similar to switch on strings?
81821
In the  library, they are overloading the () operator.  Is this a convention in C++? For callbacks, etc.?  I have seen this in code of a co-worker (who happens to be a big Boost fan). Of all the Boost goodness out there, this has only led to confusion for me.  Any insight as to the reason for this overload?