In the example below, how do I find the address of the member function f  template&lt;typename HANDLER&gt; void serialize(HANDLER &amp;h) {     //  Compiler error (gcc 4.8.1)     //  test.cxx: In function ‘void serialize(HANDLER&amp;)’:     //  test.cxx:9:26: error: expected primary-expression before ‘int’     //       auto x = &amp;HANDLER::f&lt;int&gt;;     //                            ^     //  test.cxx:9:26: error: expected ‘,’ or ‘;’ before ‘int’     auto x = &amp;HANDLER::f&lt;int&gt;; }  struct HandlerA {     template&lt;typename T&gt; void f() { } };  struct HandlerB {     template&lt;typename T&gt; void f() { } };  struct HandlerC {     template&lt;typename T&gt; void f() { } };  int main() {     HandlerA a;     HandlerB b;     HandlerC c;      a.f&lt;int&gt;();     b.f&lt;int&gt;();     c.f&lt;int&gt;();      serialize(a);     serialize(b);     serialize(c); }
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?
29017
I'm asking for a template trick to detect if a class has a specific member function of a given signature.  The problem is similar to the one cited here  but not the same: in the item of Sutter's book he answered to the question that a class C MUST PROVIDE  a member function with a particular signature, else the program won't compile. In my problem I need to do something if a class has that function, else do "something else".   A similar problem was faced by boost::serialization but I don't like the solution they adopted: a template function that invokes by default a free function (that you have to define) with a particular signature unless you define a particular member function (in their case "serialize" that takes 2 parameters of a given type) with a particular signature, else a compile error will happens. That is to implement both intrusive and non-intrusive serialization.   I don't like that solution for two reasons:   To be non intrusive you must override the global "serialize" function that is in boost::serialization namespace, so you have IN YOUR CLIENT CODE to open namespace boost and namespace serialization! The stack to resolve that mess was 10 to 12 function invocations.   I need to define a custom behavior for classes that has not that member function, and my entities are inside different namespaces (and I don't want to override a global function defined in one namespace while I'm in another one)  Can you give me a hint to solve this puzzle?
16413
How can expression functions similar to Visualforce and Lightning Aura Components be achieved within lightning web components?   For example, &lt;template if:true={index % 5 == 0}&gt;&lt;br&gt;&lt;/template&gt; expression index % 5 == 0 is not compyling at all.  accountList.html  &lt;template&gt;     &lt;template if:true={accounts.data}&gt;         &lt;template for:each={accounts.data} for:item='item' for:index='index'&gt;             &lt;!--LWC DOES NOT SUPPORT EXPRESSION FUNCTIONS--&gt;             &lt;!--&lt;template if:true={index % 5 == 0}&gt;&lt;br&gt;&lt;/template&gt;--&gt;             {item}         &lt;/template&gt;     &lt;/template&gt; &lt;/template&gt;   accountList.js  import {LightningElement, wire} from 'lwc'; import getAccounts from '@salesforce/apex/TestController.getAccounts';  export default class AccountList extends LightningElement {      @wire(getAccounts) accounts;  }   Since LWC doesn't support Expression Functions, I'd like to know how to implement them.
104370
I want to change member of structure under double pointer. Do you know how?  Example code  typedef struct {     int member; } Ttype;  void changeMember(Ttype **foo) {    //I don`t know how to do it    //maybe    *foo-&gt;member = 1; }
27491
Suppose I have this interface  public interface IFoo {     ///&lt;summary&gt;     /// Foo method     ///&lt;/summary&gt;     void Foo();      ///&lt;summary&gt;     /// Bar method     ///&lt;/summary&gt;     void Bar();      ///&lt;summary&gt;     /// Situation normal     ///&lt;/summary&gt;     void Snafu(); }   And this class  public class Foo : IFoo {     public void Foo() { ... }     public void Bar() { ... }     public void Snafu() { ... } }   Is there a way, or is there a tool that can let me automatically put in the comments of each member in a base class or interface?  Because I hate re-writing the same comments for each derived sub-class!
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?
42535
If I have a class as follows     class Example_Class     {        private:          int x;           int y;         public:           Example_Class()           {               x = 8;              y = 9;          }        ~Example_Class()         { }     };   And a struct as follows  struct {    int x;    int y; } example_struct;   Is the structure in memory of the  example_struct simmilar to that in Example_Class  for example if I do the following  struct example_struct foo_struct; Example_Class foo_class = Example_Class();  memcpy(&amp;foo_struct, &amp;foo_class, sizeof(foo_struct));   will foo_struct.x = 8 and foo_struct.y = 9 (ie: the same values as the x,y values in the foo_class) ?  The reason I ask is I have a C++ library (don't want to change it) that is sharing an object with C code and I want to use a struct to represent the object coming from the C++ library. I'm only interested in the attributes of the object.  I know the ideal situation would be to have Example_class wrap arround a common structure between the C and C++ code but it is not going to be easy to change the C++ library in use.
22097
I'm currently working on some logging code that supposed to - among other things - print information about the calling function. This should be relatively easy, standard C++ has a type_info class. This contains the name of the typeid'd class/function/etc. but it's mangled. It's not very useful. I.e. typeid(std::vector&lt;int&gt;).name() returns St6vectorIiSaIiEE.  Is there a way to produce something useful from this? Like std::vector&lt;int&gt; for the above example. If it only works for non-template classes, that's fine too.  The solution should work for gcc, but it would be better if I could port it. It's for logging so it's not so important that it can't be turned off, but it should be helpful for debugging.
16612
I have got a template class as follows:  class MyClass&lt;T&gt; {     T field;     public void myMethod()     {        field = new T(); // gives compiler error     } }   How do I create a new instance of T in my class?
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.
35378
For example:  int a = 12; cout &lt;&lt; typeof(a) &lt;&lt; endl;   Expected output:  int
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?
983
I have some template code that I would prefer to have stored in a CPP file instead of inline in the header.  I know this can be done as long as you know which template types will be used.  For example:  .h file  class foo { public:     template &lt;typename T&gt;     void do(const T&amp; t); };   .cpp file  template &lt;typename T&gt; void foo::do(const T&amp; t) {     // Do something with t }  template void foo::do&lt;int&gt;(const int&amp;); template void foo::do&lt;std::string&gt;(const std::string&amp;);   Note the last two lines - the foo::do template function is only used with ints and std::strings, so those definitions mean the app will link.  My question is - is this a nasty hack or will this work with other compilers/linkers? I am only using this code with VS2008 at the moment but will be wanting to port to other environments.
59275
Consider:  // member data omitted for brevity  // assume that "setAngle" needs to be implemented separately // in Label and Image, and that Button does need to inherit // Label, rather than, say, contain one (etc)  struct Widget {     Widget&amp; move(Point newPos) { pos = newPos; return *this; } };  struct Label : Widget {     Label&amp; setText(string const&amp; newText) { text = newText; return *this; }     Label&amp; setAngle(double newAngle) { angle = newAngle; return *this; } };  struct Button : Label {     Button&amp; setAngle(double newAngle) {         backgroundImage.setAngle(newAngle);         Label::setAngle(newAngle);         return *this;     } };  int main() {     Button btn;      // oops: Widget::setText doesn't exist     btn.move(Point(0,0)).setText("Hey");      // oops: calling Label::setAngle rather than Button::setAngle     btn.setText("Boo").setAngle(.5);  }   Any techniques to get around these problems?  Example: using template magic to make Button::move return Button&amp; or something.  edit It has become clear that the second problem is solved by making setAngle virtual.  But the first problem remains unsolved in a reasonable fashion!  edit: Well, I guess it's impossible to do properly in C++. Thanks for the efforts anyhow.
29293
Say I have classes Foo and Bar set up like this:  class Foo { public:     int x;      virtual void printStuff()     {         std::cout &lt;&lt; x &lt;&lt; std::endl;     } };  class Bar : public Foo { public:     int y;      void printStuff()     {         // I would like to call Foo.printStuff() here...         std::cout &lt;&lt; y &lt;&lt; std::endl;     } };   As annotated in the code, I'd like to be able to call the base class's function that I'm overriding. In Java there's the super.funcname() syntax. Is this possible in C++?
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?
93837
I'm looking for a piece of code that can tell me the offset of a field within a structure without allocating an instance of the structure.  IE: given  struct mstct {     int myfield;     int myfield2; };   I could write:  mstct thing; printf("offset %lu\n", (unsigned long)(&amp;thing.myfield2 - &amp;thing));   And get offset 4 for the output. How can I do it without that mstct thing declaration/allocating one?  I know that &amp;&lt;struct&gt; does not always point at the first byte of the first field of the structure, I can account for that later.