Wrappers revisited. Yuriy Solodkyy August 8, 2005

Size: px
Start display at page:

Download "Wrappers revisited. Yuriy Solodkyy August 8, 2005"

Transcription

1 Wrappers revisited Yuriy Solodkyy August 8, 2005 Abstract This essay tries to revisit idea of wrappers as presented in papers by Michael Tiemann [1] and Bjarne Stroustrup [2] with taking into account some cases that were of practical interest in problem domains I had a chance to work with. It tries to analyze pros and cons of explicit dispatching approach used by several libraries to solve the wrapping problem and then estimate what would it cost for the language to support it implicitly. I have chosen this topic to be my essay for the course, because I was really impressed by the power of explicit dispatching technique when I first saw it. I m also very interested in generic programming in C++ where I believe this approach opens new horizons for experimenting. 1 Introduction Often times we need to be able to slightly modify behavior of an existing class by adding or intercepting some functionality, while leaving the rest of the class semantics intact. Typical needs of classes that modify behavior of others can be briefly summarized as following: a) notification upon initiation and completion of a call b) access to actual parameters of a call c) access to result of a call d) access to pointer to free/member function being called e) ability to catch exceptions thrown inside called function f) ability to specialize wrappers g) ability to change type of result h) possibility to discard actual call i) vtbl level compatibility of wrapper s and original interface Here are some examples and their requirements accordingly to this list: 1. Calls dispatching in distributed systems. Requires b, c, d, e, f, i (the last for source interfaces used as callbacks). 2. Synchronization of access to shared objects in multithreaded environments Requires a (in simple cases), d (in more complicated synchronization models) and f (e.g. to let const methods lock for reading and non-const for writing) 3. Support of Aspect Oriented Programming 4. Transcoding error handling models (status codes, OS level exceptions, C++ exceptions, last error descriptors etc.) Requires c, g, e 1

2 5. Code instrumentation (timing, debug tracing, error handling etc.) Requires d, b, c and e (may be useful to intercept from where exception was thrown) 6. Memoization Requires d, b, c, h 7. Proxy objects that try to behave as a reference to another object Requires d, b, c, g 2 Related works A very good overview and historical backgrounds of the problem are given in [2]. To conserve space we ll just list main features and shortcomings of approaches mentioned there. Call and Return facilities of C with Classes [3] allowed defining a pair of functions that would implicitly be called before every call of every member function (except constructors) and before every return from every member function (except destructor). Construct was removed from the language as almost no one was using it, though it would have easily solved us cases 2 and partially 5 if it was a day. The notion wasn t perfectly general for example, you couldn t access arguments for a member function call from call(), which are required to handle cases 1,4,5 and 6. C++ language extension called Wrappers proposed by Michael Tiemann [1] was intended to allow the programmer to specify an alternate implementation of member function calls and was based on notion that in order to execute a member function call, it is necessary only to know the object for the call, member function for that object and arguments. This proposal had the interesting property that the wrapper had access to the arguments and the return value of a call. Also, different wrapper functions could be provided for functions with different types. Approach was not adopted though as there were other serious problems: Approach was intrusive as it required controlled classes to be derived from a specific base class. General case of wrapping any method was handled via untyped argument list (...) which was type unsafe and required compiler tricks to be implemented. Run-time overhead for possibility of ignoring wrapping of certain methods. Author argues though that this could have been optimized out by a good compiler via inlining and constant folding. Compiler had to treat differently this-pointer inside a wrapper because implicit conversion of a pointer-toan-object to a pointer-to-a-base-class of that object is anti-symmetric with respect to pointers to member functions and hence it was not type safe to pass a pointer-to-a-derived-member-function to a wrapper in a base class. To overcome this deficiency compiler had to check whenever possible whether this-pointer actually points to an object of derived class and whenever not, it had to provide appropriate run-time check. If implemented, this extension would have probably been able to solve most of our cases, though cost of supporting such a feature in C++ compiler would have been significant. In 2000 Bjarne Stroustrup presented partial solution to a wrapping problem [2]. This solution was non intrusive and didn t require any language changes hence could be implemented in Standard C++. Some language features, used in implementation, were not available at the time Michael Tiemann presented his ideas though. Used technique has the nice property that the suffix code is executed even if method throws an exception. Idea was capable of solving case 2 and certain subcases of 3 and 5. What prevents it from solving other cases can be best quoted from Dr. Stroustrup s paper: A fundamental limitation of this prefix/suffix approach is that neither prefix nor suffix has access to the arguments to the called function or the result produced. For example, it is not possible to provide a prefix/suffix to record the results of calls or to map calls into message send operations. In this, the prefix/suffix approach differs from the Tiemann wrappers [1]. Another limitation of this approach is that the prefix and suffix are independent. For example, it is not possible to write a prefix/suffix pair that catch exceptions thrown by the wrapped functions. It is not possible to open a try-block in the prefix and close it in the suffix. 2

3 However, it is possible for a prefix and a suffix to communicate. A simple use of that is for a prefix and a suffix to access the same variable as is typically done for locks and trace state. To do the call, Wrap and Call proxy get a pointer to the object called. Thus, they might keep a record of objects accessed, but they do not know which function is called for those objects. [2] Also unlike Tiemann s proposal this technique didn t allow specializing wrappers for methods with different signatures, only for different classes. This may be important for example in RPC domain where you may want to specialize wrapping of methods that return void to not block before the result is returned. Another, though smaller inconvenience of this approach lays in fact that its usage assumes some sort of pointer semantics, which is not always desirable or possible. As an example you may think of wrapper atomic<t> which synchronizes access to members of T on one-method-at-a-time bases. We want atomic<t> to behave exactly as T does (to the extent implementable in current C++) but have synchronization semantics mentioned above. 3 Dispatchers In a search for greater flexibility some libraries adopted approach of explicit function call wrapping [4, 5]. In these libraries family of parameterized free functions or parameterized member functions of a dedicated wrapper class explicitly accept what to call and on which arguments. Term dispatchers is probably not the most appropriate here, - we picked it by analogy from COM/DCOM [6] where it is used for similar purposes, though without direct relation to C++ templates. We didn t use term forwarding function as defined in [7] to highlight the fact that apart from forwarding parameters, dispatchers also forward pointer to free/member function. The following example demonstrates idea of dispatchers by implemententing redirection of calls to an actual object represented by reference, access pattern though can be more sophisticated. To conserve space we ll also omit explicit calls to prefix, suffix as well as wrap calls into try-catch blocks, however they are assumed in most of the places. template <class T> class dispatcher public: dispatcher(t& obj) : m_obj(obj) } template <class R> R dispatch(r (T::*method)() const) return (m_obj.*method)(); } template <class R> R dispatch(r (T::*method)() ) return (m_obj.*method)(); } template <class R, class A1> R dispatch(r (T::*method)(A1) const, A1 a1) return (m_obj.*method)(a1); } template <class R, class A1> R dispatch(r (T::*method)(A1), A1 a1) return (m_obj.*method)(a1); } template <class R, class A1, class A2> R dispatch(r (T::*method)(A1, A2) const, A1 a1, A2 a2) return (m_obj.*method)(a1, a2); } template <class R, class A1, class A2> R dispatch(r (T::*method)(A1, A2), A1 a1, A2 a2) return (m_obj.*method)(a1, a2); } // --- etc. private: T& m_obj; // Underlain object to redirect calls to 3

4 Amount of arguments that can be handled by the wrapper is typically limited by a predefined constant, big enough to suit practical needs. Calls in this scenario are made as following: class MyClass //... int f() const; int g(const string& s); double h(float f, long n); string m(const char* s) const; template <class U> U z(u u); MyClass my; dispatcher<myclass> dsp(my); int i = dsp.dispatch(&myclass::f); int j = dsp.dispatch(&myclass::g, "aaa"); int k = dsp.dispatch(&myclass::z<int>, 7); std::cout << dsp.dispatch(&myclass::h, , 42) << std::endl; We could have overloaded operator->* of dispatcher to return a functor and as a result let the calling syntax look like: int i = (dsp->*&myclass::f)(); int j = (dsp->*&myclass::g)("aaa"); int k = (dsp->*&myclass::z<int>)(7); however we didn t do that on purpose to avoid tying dispatcher to pointer semantics only. While this is perfect for some of our use cases, this is not suitable for all of them. This will become clearer in the next section while example of this approach with overloading operator->* can be found in my second essay for this course. Another possibility was to use operator() instead of method dispatch, however such usage intuitively does not assume passing pointer to member function to call as a first argument. At the same time giving an explicit name to such method allows us to use it in global scope for dispatching of free functions and functors: template <class R> R dispatch(r (*func)()) return func(); } template <class R, class A1> R dispatch(r (*func)(a1), A1 a1) return func(a1); } template <class F> typename result_of<f()>::type dispatch(f f) return f(); } template <class F, class A1> typename result_of<f(a1)>::type dispatch(f f, A1 a1) return f(a1); } Where result_of<> is oncoming standard library s solution to the problem of obtaining result type of applying function object to a given set of argument types. [8]. Calls will be dispatched similarly: int my_func(double d); int n = dispatch(&my_func, 0.0); 4

5 Interesting property of dispatching approach is that it also allows wrapping access to data members. This has some limitations though, but works well in majority of typical cases. template <class T> class dispatcher //... previous definitions skipped... template <class V> struct read_write_proxy read_write_proxy(t& t, V T::*attribute) : m_obj(t), m_attr(attribute) } V operator=(const V& val) return m_obj.*m_attr = val; } operator V() const return m_obj.*m_attr; } private: T& m_obj; V T::*m_attr; template <class V> read_write_proxy<v> dispatch(v T::*attribute) return read_write_proxy<v>(m_obj, attribute); } Note, we return here a proxy object rather then direct reference to a member because in the most interesting cases of this approach s applications, member is not available directly and either needs to be brought in for reading or updated with new value written. Exactly this situation occurs in RPC domain, where we may refer to a member of an object located remotely. Because proxy is also a kind of wrapper, that tries to behave as a reference to a data member, it naturally has limitations of class wrapping we are trying to analyze in this essay. For example in the following snippet: class MyClass //... previous definitions skipped... public: int m_n; const double m_d; std::string m_s; dsp.dispatch(&myclass::m_n) = 5; std::cout << dsp.dispatch(&myclass::m_n) << std::endl; std::cout << dsp.dispatch(&myclass::m_d) << std::endl; dsp.dispatch(&myclass::m_s) = "Howdy"; all dispatchings work just fine, however already: std::cout << dsp.dispatch(&myclass::m_s) << std::endl; fails to compile because in C++ overload resolution happens after template argument deduction, and this deduction does not consider conversion from read_write_proxy<std::string> to std::string. Template argument deduction is required here because operator<< is defined for generic templates std::basic_ostream<> and std::basic_string<>. Should standard library have defined specialization of operator<< for types std::ostream and std::string, which are just instantiations of these templates, overload resolution would have picked it up using our conversion from read_write_proxy<std::string> to std::string. Without this we have to either use explicit cast: std::cout << static_cast<std::string>(dsp.dispatch(&myclass::m_s)) << std::endl; 5

6 or add to read_write_proxy operator<< defined on basic_ostream<> that simply redirects call to parameter s operator<<. template<class _Char, class _Traits> friend std::basic_ostream<_char, _Traits>& operator<<( std::basic_ostream<_char, _Traits>& os, const read_write_proxy& p) return os << static_cast<v>(p); } This, however, neither covers general case (we only consider operator<< for std::basic_ostream<>) nor can be done for all the operators that can be overloaded in C++ as this would require deduction of type from a result of operation. Hopefully next revision of C++ is going to cover this issue with decltype operator [9]. To the extent I understand the proposal, what even decltype won t be able to fix is possibility to say dsp.dispatch(\&myclass::m_s).empty() without explicit casting a la static_cast<std::string>(dsp.dispatch(&myclass::m_s)).empty() (assuming that dispatch returns proxy object rather then reference). 3.1 Pluses and minuses Despite syntactical changes that need to be done to all function calls, dispatching approach has most of the nice properties referred in Tiemann s and Stroustrup s work: a) Access to arguments of call and return value b) Possibility to specialize dispatcher for methods/free functions with given signature c) Complete control over call possibility to discard actual call or execute it several times possibility to change the value and/or type of returned result possibility to catch exceptions in actual call d) Non intrusiveness can wrap objects not written by us allows to make a direct call instead of a wrapped one e) Compile time wrapping logic preselection. This is different from the wrapper predicate proposed by Tiemann though, as because of distinct call syntax we do not really abolish wrapping but rather choose between simple wrapping (redirect call without doing anything else) and purposeful wrapping (does other things apart from call redirection). This can be achieved for example via boost::enable if template [10] f) Possibility to dispatch methods of different classes g) Possibility to wrap access to data members h) Possibility to deduct return type and types of arguments from a single argument - pointer to free/member function. Approach has negative sides too: a) Difference in calling syntax makes approach less generic as code written without it in mind won t be able to benefit from wrappers possibilities. INVOKE proposed in [11] can possibly help with this issue. 6

7 b) When wrapped class uses function overloading, calling becomes even more tedious: class MyClass //... double h(float f, long n); int h(short f, bool b); dsp.dispatch(static_cast<int (MyClass::*)(short,bool) const>(&myclass::h), 8, true); int (MyClass::*pmf)(short,bool) const = &MyClass::h; // second option dsp.dispatch(pmf, 8, true); as otherwise taking address of the method is ambiguous. c) Call dispatching is tightly linked to call forwarding as described in [7] and naturally has all its problems. I will abstract from those issues here as oncoming revision of C++ standard will likely provide means to handle those. [12, 13] 4 Anonymous members If it was not for the syntax, dispatchers would have been a very good solution to the problems we mentioned. In what follows we try to merge Tiemann s idea with ideas seen in dispatchers. This is not a language extension proposal as it has too many unclarities and lacks formalism required by [14]. Instead I regard it as an exercise in evaluating language feature, hoping that this experience may become useful later in the course. As can be noticed, when using dispatchers we ve got to duplicate some of the information twice. In the example above we refer to MyClass first in type within variable definition dispatcher<myclass>dsp(my); and then in every call we make as a part of the pointer-to-member reference dsp.dispatch(&myclass::g, "aaa");. It seems to be logical to leave the first reference, as we want to be able to explicitly specify which classes to wrap and which not, but as to the second one, it would have probably been nicer to have possibility in the language to bring it into the context. Borrowing syntax from Tiemann s work this should probably look as following: template <class T> class dispatcher public: dispatcher(t& obj) : m_obj(obj) } template <class R, class A1, class A2> R ()T (R (T::*method)(A1, A2), A1 a1, A2 a2) return (m_obj.*method)(a1, a2); } private: T& m_obj; Again, term anonymous member is probably not the right one here, but we need another name as it s Tiemann s alternative wrapper is used in several contexts: Class that wraps functionality of another class Method of a class that wraps certain set of methods of a wrapped class Name Unnamed or anonymous member reflects the fact that member s name serves purpose of a formal argument, and not a real member name within its class. Another possibility would have probably been to call it a meta member as it works on regular members of a class, however this is not truly compile-time work, as what they define will be executed at run-time. Leaving naming aside, the user of dispatcher class will be writing code as following: 7

8 MyClass my; dispatcher<myclass> dsp(my); int i = dsp.f(); // invokes dsp.()t(&t::f) int j = dsp.g("aaa"); // invokes dsp.()t(&t::g, "aaa") Informal reasoning behind this should probably sound as following: Class dispatcher doesn t have any method g(), but has some anonymous members, which can be invoked with one argument, thus for each of them compiler should bring for overload resolution all the methods g() of classes referred in the signature of matching anonymous members. Note, dispatchers presented in the previous paragraph were able to dispatch calls from multiple classes and therefore it seems unreasonable to abolish this possibility for anonymous members (unless we have a strong reasoning why). This may look as following: template <class T, class U> class dispatcher public: dispatcher(t& obj1, U& obj2) : m_obj1(obj1), m_obj2(obj2) } template <class R, class A1> R ()T (R (T::*method)(A1), A1 a1) return (m_obj1.*method)(a1); } template <class R, class A1> R ()U (R (U::*method)(A1), A1 a1) return (m_obj2.*method)(a1); } private: T& m_obj1; U& m_obj2; With this said while looking for an appropriate method to call, compiler should bring for overload resolution (into viable overload set) methods with given name from classes mentioned in all the anonymous members that can possibly satisfy this call. Overload resolution is then performed in accordance to existing C++ overloading rules (13.3 in [15]). 4.1 Various issues Already after a brief look at this feature several questions arise 1. Should regular methods of a wrapper class get priority over methods brought in via anonymous members or should they all be equal in overload resolution? 2. How to wrap overloaded methods with the same amount of arguments but different signatures? Should compiler in this case tries to deduct argument types from the call? 3. What about default arguments? Should anonymous member s default arguments be used or actual method s? 4. Can wrappers be used transparently in polymorphic calls? 5. Can call to a free function be wrapped the same way? 6. How to define behavior in case of dispatching calls from many classes when types are related (say T=U in the example above) 7. Which wrapper should handle the call when method s signature matches two wrappers? 8. How about template members of a wrapped class? 8

9 9. Hidden problems with sizeof(t) in generic code, since sizeof wrapper needn t be the same as sizeof wrapped. 1. It seems to be logical to have regular methods of a wrapper be brought to a viable set for overload resolution together with matching methods of the wrapped class. In case of ambiguities though regular methods of a wrapper should probably be chosen as a better match to let user override wrapping behavior of some methods. 2. Overloaded methods with the same amount of arguments were posing problem in case of explicit dispatching as getting pointer to a member was ambiguous. Here situation is a little bit different from explicit dispatching though. In explicit dispatching arguments of the call were separated from the method to call and even though we were using them as such, compiler knew nothing about our intent. In case of anonymous members compiler knows that arguments are for the call with pointer-to-member it s going to deduce and hence it can use those arguments to deduct types of pointer-to-member s arguments. 3. It seems to be reasonable to allow default parameters for arguments of anonymous members since one may probably want to create a wrapper with the sole purpose of providing defaults for arguments without it in some original methods. Nevertheless, because in C++ a template type parameter cannot be deduced from the type of a function default argument ( (17) in [15]) we can t use meta method s default arguments to deduce argument s type. At the same time default arguments of actual member function may affect resolution of anonymous member and then should be used as arguments to its call. 4. Why would someone need vtbl level compatibility of wrapper and wrapped object? When making a remote call, as one of the parameters caller may supply pointer to its interface for callbacks. In such scenario we want to be able to generate a wrapper (called stub in COM/DCOM [6]) that has exactly the same vtbl layout as caller object but generic implementations of virtual methods that marshal calls back to the caller s processor. We need to understand here, that virtual methods of the wrapped class and anonymous methods of a wrapper (even if they are virtual) have different signatures because of pointer to member passed in and hence are not compatible for polymorphic calls. What can be done in this case is the following: struct MyInterface virtual int A(int) = 0; virtual void B(double) = 0; struct MyClass : public MyInterface int A(int); void B(double); template <class T> class wrapper : public MyInterface int A(int a) return T::() A(a); } // Redirect to wrapper. This will wrap T::A() void B(double b) return T::() B(b); } // Syntax from Tiemann s work template <class R, class A1> R ()T (R (T::*method)(A1), A1 a1) return (m_obj1.*method)(a1); } MyClass my; wrapper<myclass> dsp(my); void f(myinterface* pmyinterface); f(&dsp); Such manual redirection covers case with callbacks, however it assumes that callee won t be querying another interface via dynamic cast<>, as wrapper won t necessarily implement it too. In this scenario we don t really need 9

10 anonymous method as since we explicitly implement methods of the interface, we can do the necessary wrapping inside them. More general wrapping for this case requires more issues to be analyzed and will be skipped for now. 5. With anonymous members we in a way brought certain members of one class into the scope of another. Class name of the first class plays role of boundary of what to bring in. Doing the same with free functions will be too permissive and would have too much overhead in cases when we wanted to wrap a limited set of functions but wrapped many more because we didn t think they would also match the template. At the same time if we think of static member functions, which are treated similarly to free functions in C++, one may think of an anonymous member (say static anonymous member) that wraps static members of a class. Having made this step one naturally would want to extend the idea to namespaces, but we miss important differences here: Call to a static member will be wrapped not unconditionally, but when made through a wrapper (e.g. call MyClass::static member() won t be wrapped, while call wrapper<myclass>::static member() will be) Unlike classes in C++, namespaces can be reopened. This may create ambiguities when adding new wrappers to namespace as they may conflict with existing ones. On the other hand this may change wrapping logic in different translation units. 6. When types are related by hierarchy, we might get the following situation: template <class B, class D> struct dispatcher template <class R, class A1, class A2> R ()B (R (B::*method)(A1, A2), A1 a1, A2 a2) return (m_objb.*method)(a1, a2); } template <class R, class A1, class A2> R ()D (R (D::*method)(A1, A2), A1 a1, A2 a2) return (m_objd.*method)(a1, a2); } struct B int a(int, int); int b(int, int); struct D : public B int b(int, int); int c(int, int); B b; D d; wrapper<b,d> bd(b,d); bd.a(1,2); // B::() a(1,2); or D::() a(1,2); or ambiguous? bd.b(3,4); // B::() B::b(3,4); or D::() D::b(3,4); or ambiguous? bd.c(5,6); // D::() c(5,6) Answer seems to be obvious only for the last case: anonymous member for derived class should clearly be selected for methods of Derived not found in Base. There seems to be no clear preference in the second case, when b() is overloaded in derived. In the first case anonymous member for B seems to be a better match, though this has to be yet formalized. 10

11 Case when both classes of the wrapper are the same needn t be ambiguous by default: for example anonymous members of the first parameter may cover only members with even amount of arguments while anonymous members of the second parameter - with odd. In such scenario calling method on wrapper<t,t> won t be ambiguous as only one anonymous member can be used. In those cases when anonymous members from both parameters overlap in what they can cover, designer of a wrapper class template can always specialize her class to cover this case. 7. When method s signature matches two wrappers, call is ambiguous and can be disambiguated via explicit call to appropriate anonymous member. 8. When wrapped class has member templates, in dispatching approach we had to instantiate it explicitly before taking address of the method. As before, this was because compiler didn t know about any relationship between the first argument (member function pointer) and other arguments of dispatch method. Here situation is different and since compiler is trying to use anonymous method, it knows, that arguments are for call with the member function pointer it is going to deduce and hence it can use types of those arguments to deduce parameters of member template (of course only in those cases where these types can be deduced from parameters of call to member template). 9. Usage of sizeof in generic code can clearly cause problems when wrappers are used, however it doesn t seem to be unsafe as with idea of overloading sizeof() itself. 4.2 Tiemann s issues Apart from these questions let s look at applicability of the issues from Tiemann s paper to our situation [1]. 1. How does one keep a specific function call from being wrapped? 2. How does one keep a particular member function from being wrapped? 3. What happens when passing a derived member function to a base wrapper? 4. How does one handle type conversion in an argument list full of...? 5. Should constructors be wrapped? 6. Should wrappers be recursive? (can call to a wrapper be wrapped?) 7. Should function calls within wrappers be wrapped? (where is the bottom?) 8. How does one wrap a function call via a pointer-to-function (which may be missing type information)? 9. How should return values be handled? (overload based on return types?) 1. When logic of wrapper requires some of the calls to be made directly, such wrapper can expose a method to get reference to the original object, via which all methods can be called directly. A good option in some cases for such a method can be operator-> defined on wrapper. 2. Second question should be elaborated before it can be applied to our case. In Tiemann s work classes were derived from base classes that implemented wrappers and hence indirectly wrappers and wrapped methods were parts of a derived class. In our case wrapper and wrapped objects are not bound by inheritance and hence methods of a wrapper are called on wrapper, while methods of a wrapped class are called on wrapped object. To have a different implementation of a particular method or to wrap it up differently, wrapper always can implement method with the same signature, which should probably be given priority in overload resolution since it doesn t involve call to an anonymous member. As usual declaring method private on wrapper will make it inaccessible. 3. Again, since wrapper and wrapped class are not necessarily bound by inheritance relation, question is not directly applicable. When they are (D : wrapper<b> : B), passing a derived member function to a base wrapper violates anti-symmetry of implicit conversion between pointers to member functions and hence is not type safe. Another case when D is derived from B and wrapper<b> is initialized with reference to D won t allow wrapping of D s members for the same reason. Employing Curiously Recurring Template Pattern (D : wrapper<d>) will be type safe as wrapper will see D s definition [16]. 11

12 4. Unfortunately I don t have enough knowledge on details of ellipses handling in C++ so I ll skip this one. 5. Wrapping constructors/destructor makes sense in our case as construction/destruction of wrapper needn t be directly linked to construction destruction of a wrapped object It certainly makes sense in remote procedure calling where object may be created on one processor by the caller on another. In this case we want to be able to marshal parameters of creation from caller s process to the process where object will reside. This is perfectly possible with dispatchers so there seems to be no reason of disallowing it on wrappers. 6. There seems to be no obvious reason why calls to wrappers shouldn t be wrapped by other wrappers. For any instantiation of a wrapper with a given type(s) we know exactly set of methods and their signatures, available on wrapper. This can be computed as a filtering of parameter type s methods that match any of the meta methods. Another interesting question though is whether it should be possible to write a meta method of higher order that wraps calls to regular meta methods. Are of application of those should probably be related to area of template template parameters application, however I can t directly find such for higher order meta methods. 7. Question about whether function calls within wrapper should be wrapped is not as important to our case as it was for Tiemann s case: because of inheritance, there wrapper s methods and wrapped methods were becoming part of the same class, while here they are members of distinct classes (actually even more: members of a class and a class template). 8. When call is made through pointer-to-member, compiler s task becomes simpler: it needn t do any overload resolution, just find a wrapper that matches type of the pointer-to-member and call it with provided pointer. 9. Dispatchers presented before had a full control over return type and could change it if needed. For example they could have returned a type that is a wrapper around original return type. The same seems to be possible here as return type is deduced from pointer-to-member passed in. Hence this won t require overload based on return types. 5 Examples of usage In this section we ll try to briefly demonstrate intended use of anonymous members. 5.1 Synchronization template <class T, class SyncModel=default_sync_policy<T> > class atomic //... template <class R, class A1> R ()T (R (T::*method)(A1), A1 a1) auto_lock<syncmodel> Lock(m_obj); // will unlock in destructor return (m_obj.*method)(a1); } 5.2 Instrumentation template <class T> class logger //... template <class R, class A1, class A2> R ()T (R (T::*method)(A1, A2), A1 a1, A2 a2) std::clog << typeid(t).name() << " s method called with arguments " << a1 << " and " << a2 << std::endl; std::clock_t start = clock(); R r = (m_obj.*method)(a1, a2); std::clock_t finish = clock(); 12

13 } std::clog << typeid(t).name() << " s method called with arguments " << a1 << " and " << a2 << " returned " << r << " execution time " << double(finish - start)/clocks_per_sec << std::endl; return r; 5.3 Error handling template <class T> class status_to_exception //... template <class A1, class A2> HRESULT ()T (HRESULT (T::*method)(A1, A2), A1 a1, A2 a2) HRESULT hr = (m_obj.*method)(a1, a2); if (FAILED(hr)) throw com_exception(hr); return hr; } Another usage example, implemented by means of standard C++ via explicit dispatching, can be found in my second essay. 5.4 Conclusions Generalizing wrappers based on ideas seen in explicit dispatchers seems to be an attractive idea for generic programming purposes, however it poses too many questions yet to be answered before it can be considered. In this respect it may also be interesting to compare issues of this approach with those that appear with overloading of operator. and see whether both can t be resolved in yet more generic way. These and similar issues related to generics in C++ is what drives my interest in C++ Language Design Studio course, where I hope to learn more on how to tackle them. References [1] Michael Tiemann. wrappers: solving the rpc problem in gnu c++. In USENIX C++ Conference, pages , Denver, CO, October [2] Bjarne Stroustrup. Wrapping c++ member function calls. C++ Report, 12(6), June [3] B. Stroustrup. The Design and Evolution of C++. Addison Wesley, March ISBN [4] Douglas Gregor. The Boost Function library. June [5] Lawrence Rauchwerger Steven Saunders. Armi: An adaptive, platform independent communication library. In ACM SIGPLAN Symp. Prin. Prac. Par. Prog. (PPOPP), page 12, San Diego, CA, June [6] Richard T. Grimes. Professional ATL COM Programming. Wrox Press, September ISBN [7] Peter Dimov, Howard Hinnant, and Dave Abrahams. The forwarding problem: Arguments. In ANSI/ISO C++ Standard Committee Pre-Santa Cruz mailing, number N1385= in ANSI/ISO C++ Standard Committee Pre- Santa Cruz mailing, October [8] Douglas Gregor. A uniform method for computing function object return types. In ANSI/ISO C++ Standard Committee Post-Oxford mailing, number N1454= in ANSI/ISO C++ Standard Committee Post-Oxford mailing, April [9] Jaakko Järvi and Bjarne Stroustrup. Decltype and auto (revision 3). In ANSI/ISO C++ Standard Committee Pre-Sydney mailing, number N1607= in ANSI/ISO C++ Standard Committee Pre-Sydney mailing,

14 [10] J. Järvi, J. Willcock, H. Hinnant, and A. Lumsdaine. Function overloading based on arbitrary properties of types. C/C++ Users Journal, 21(6):25 32, June [11] Peter Dimov Pete Becker. Unifying tr1 function object type specifications. In ANSI/ISO C++ Standard Committee Pre-Redmond mailing, number N1673= in ANSI/ISO C++ Standard Committee Pre-Redmond mailing, September [12] Howard E. Hinnant, Peter Dimov, and Dave Abrahams. A proposal to add move semantics support to the C++ language. In ANSI/ISO C++ Standard Committee Pre-Santa Cruz mailing, number N1377= in ANSI/ISO C++ Standard Committee Pre-Santa Cruz mailing, October [13] Peter Dimov Howard E. Hinnant, Dave Abrahams. A proposal to add an rvalue reference to the c++ language. In ANSI/ISO C++ Standard Committee Pre-Redmond mailing, number N1690= in ANSI/ISO C++ Standard Committee Pre-Redmond mailing, September [14] Members of the X3J16 working group on extensions. How to write a c++ language extension proposal for ansix3j16/iso-wg21. In ACM SIGPLAN Notices, volume 27, June [15] ISO/IEC International Standard. Programming languages C++. American National Standards Institute, September [16] James O. Coplien. Curiously recurring template patterns. C++ Rep., 7(2):24 27,

G52CPP C++ Programming Lecture 20

G52CPP C++ Programming Lecture 20 G52CPP C++ Programming Lecture 20 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Wrapping up Slicing Problem Smart pointers More C++ things Exams 2 The slicing problem 3 Objects are not

More information

Part X. Advanced C ++

Part X. Advanced C ++ Part X Advanced C ++ topics Philip Blakely (LSC) Advanced C++ 158 / 217 References The following are highly regarded books. They are fairly in-depth, and I haven t read them in their entirity. However,

More information

Bounding Object Instantiations, Part 2

Bounding Object Instantiations, Part 2 This is a pre-publication draft of the column I wrote for the June 1995 issue of the C++ Report. Pre-publication means this is what I sent to the Report, but it may not be exactly the same as what appeared

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

Decltype (revision 6): proposed wording

Decltype (revision 6): proposed wording Decltype (revision 6): proposed wording Programming Language C++ Document no: N2115=06-0185 Jaakko Järvi Texas A&M University College Station, TX jarvi@cs.tamu.edu Bjarne Stroustrup AT&T Research and Texas

More information

American National Standards Institute Reply to: Josee Lajoie

American National Standards Institute Reply to: Josee Lajoie Accredited Standards Committee X3 Information Processing Systems Operating under the procedures of American National Standards Institute Doc No: X3J16/95-0051 WG21/N0651 Date: March 3, 1995 Page 1 of 15

More information

Defining default copy and move

Defining default copy and move N2904=09-0094 2009-06-18 Bjarne Stroustrup Defining default copy and move Abstract This note examines the generation of default copy and move operations when elements are combined into an aggregate. It

More information

C The new standard

C The new standard C++11 - The new standard Lars Kühne Institut für Informatik Lehrstuhl für theoretische Informatik II Friedrich-Schiller-Universität Jena January 16, 2013 Overview A little bit of history: C++ was initially

More information

Implicit Evaluation of auto Variables and Arguments

Implicit Evaluation of auto Variables and Arguments Implicit Evaluation of auto Variables and Arguments Document number: N3748 Authors: Joël Falcou University Paris XI, LRI Peter Gottschling SimuNova Herb Sutter Microsoft Date: 2013-08-30 Project: Programming

More information

Overview. 1. Expression Value Categories 2. Rvalue References 3. Templates 4. Miscellaneous Hilarity 2/43

Overview. 1. Expression Value Categories 2. Rvalue References 3. Templates 4. Miscellaneous Hilarity 2/43 Advanced C++ 1/43 Overview 1. Expression Value Categories 2. Rvalue References 3. Templates 4. Miscellaneous Hilarity 2/43 1. Expression Value Categories These are not the droids you re looking for ~Obi-wan

More information

I m sure you have been annoyed at least once by having to type out types like this:

I m sure you have been annoyed at least once by having to type out types like this: Type Inference The first thing I m going to talk about is type inference. C++11 provides mechanisms which make the compiler deduce the types of expressions. These features allow you to make your code more

More information

Implementing Object Equivalence in Java Using the Template Method Design Pattern

Implementing Object Equivalence in Java Using the Template Method Design Pattern Implementing Object Equivalence in Java Using the Template Method Design Pattern Daniel E. Stevenson and Andrew T. Phillips Computer Science Department University of Wisconsin-Eau Claire Eau Claire, WI

More information

Concepts syntax and composition

Concepts syntax and composition N1536=03-0119 October 22, 2003 Concepts syntax and composition Bjarne Stroustrup (bs@research.att.com) Gabriel Dos Reis (gdr@acm.com) Abstract Assume that we have a basic notion of a concept. A concept

More information

Wording for lambdas in unevaluated contexts

Wording for lambdas in unevaluated contexts Wording for lambdas in unevaluated contexts Document #: P0315R4 Date: 2017-11-10 Project: Programming Language C++ Audience: Core Working Group Reply-to: Louis Dionne Hubert Tong

More information

Deducing the type of variable from its initializer expression (revision 3)

Deducing the type of variable from its initializer expression (revision 3) Deducing the type of variable from its initializer expression (revision 3) Programming Language C++ Document no: N1894=05-0154 Jaakko Järvi Texas A&M University College Station, TX jarvi@cs.tamu.edu Bjarne

More information

Structured bindings with polymorphic lambas

Structured bindings with polymorphic lambas 1 Introduction Structured bindings with polymorphic lambas Aaryaman Sagar (aary800@gmail.com) August 14, 2017 This paper proposes usage of structured bindings with polymorphic lambdas, adding them to another

More information

Item 4: Extensible Templates: Via Inheritance or Traits?

Item 4: Extensible Templates: Via Inheritance or Traits? ITEM1_11new.fm Page 1 Tuesday, November 27, 2001 12:49 PM Item 4: Extensible Templates: Via Inheritance or Traits?. ITEM 4: EXTENSIBLE TEMPLATES: VIA INHERITANCE OR TRAITS? DIFFICULTY: 7 This Item reviews

More information

Deducing the type of variable from its initializer expression (revision 4)

Deducing the type of variable from its initializer expression (revision 4) Deducing the type of variable from its initializer expression (revision 4) Programming Language C++ Document no: N1984=06-0054 Jaakko Järvi Texas A&M University College Station, TX jarvi@cs.tamu.edu Bjarne

More information

Increases Program Structure which results in greater reliability. Polymorphism

Increases Program Structure which results in greater reliability. Polymorphism UNIT 4 C++ Inheritance What is Inheritance? Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the

More information

Stroustrup Default comparisons N4175. Default comparisons. Bjarne Stroustrup

Stroustrup Default comparisons N4175. Default comparisons. Bjarne Stroustrup WG21-N4175 2014-10-11 Default comparisons Bjarne Stroustrup (bs@ms.com) Abstract Defining comparison operators (==,!=, =) for simple classes is tedious, repetitive, slightly error-prone,

More information

Wrapping a complex C++ library for Eiffel. FINAL REPORT July 1 st, 2005

Wrapping a complex C++ library for Eiffel. FINAL REPORT July 1 st, 2005 Wrapping a complex C++ library for Eiffel FINAL REPORT July 1 st, 2005 Semester project Student: Supervising Assistant: Supervising Professor: Simon Reinhard simonrei@student.ethz.ch Bernd Schoeller Bertrand

More information

Object-Oriented Programming

Object-Oriented Programming - oriented - iuliana@cs.ubbcluj.ro Babes-Bolyai University 2018 1 / 56 Overview - oriented 1 2 -oriented 3 4 5 6 7 8 Static and friend elements 9 Summary 2 / 56 I - oriented was initially created by Bjarne

More information

Extended friend Declarations

Extended friend Declarations Document number: Date: 19 September, 2003 Reply to: William M. Miller The MathWorks, Inc. wmm@world.std.com Extended friend Declarations I. The Problem According to the current Standard, 11.4 2, An elaborated-type-specifier

More information

VIRTUAL FUNCTIONS Chapter 10

VIRTUAL FUNCTIONS Chapter 10 1 VIRTUAL FUNCTIONS Chapter 10 OBJECTIVES Polymorphism in C++ Pointers to derived classes Important point on inheritance Introduction to virtual functions Virtual destructors More about virtual functions

More information

Multiple and Virtual Inheritance in C++

Multiple and Virtual Inheritance in C++ Multiple and Virtual Inheritance in C++ Initialization of virtual bases Final classes in C++ Multiple inheritance, virtual members and virtual inheritance. Construction order with multiple inheritance

More information

PIC 10A Objects/Classes

PIC 10A Objects/Classes PIC 10A Objects/Classes Ernest Ryu UCLA Mathematics Last edited: November 13, 2017 User-defined types In C++, we can define our own custom types. Object is synonymous to variable, and class is synonymous

More information

Improving Enumeration Types [N1513= ] David E. Miller

Improving Enumeration Types [N1513= ] David E. Miller Introduction Improving Enumeration Types [N1513=03-0096] David E. Miller It has been said, "C enumerations constitute a curiously half-baked concept." [Design and Evolution of C++ (C) 1994, B.Stroustrup,

More information

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 7 September 21, 2016 CPSC 427, Lecture 7 1/21 Brackets Example (continued) Storage Management CPSC 427, Lecture 7 2/21 Brackets Example

More information

Modules: ADL & Internal Linkage

Modules: ADL & Internal Linkage Document Number: p1347r1 Date: 2019-01-17 To: SC22/WG21 EWG Reply to: Davis Herring, Nathan Sidwell herring@lanl.gov, nathan@acm.org Re: p1103r1 Merging Modules Modules: ADL & Internal Linkage Nathan Sidwell,

More information

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things. A Appendix Grammar There is no worse danger for a teacher than to teach words instead of things. Marc Block Introduction keywords lexical conventions programs expressions statements declarations declarators

More information

Let return Be Direct and explicit

Let return Be Direct and explicit Document #: N4029 Date: 2014-05-23 Reply to: Herb Sutter hsutter@microsoft.com Let return Be Direct and explicit Herb Sutter This paper addresses EWG issue #114. Discussion C++ already recognizes that

More information

Interview Questions of C++

Interview Questions of C++ Interview Questions of C++ Q-1 What is the full form of OOPS? Ans: Object Oriented Programming System. Q-2 What is a class? Ans: Class is a blue print which reflects the entities attributes and actions.

More information

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Cpt S 122 Data Structures. Course Review Midterm Exam # 2 Cpt S 122 Data Structures Course Review Midterm Exam # 2 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 2 When: Monday (11/05) 12:10 pm -1pm

More information

Python in the Cling World

Python in the Cling World Journal of Physics: Conference Series PAPER OPEN ACCESS Python in the Cling World To cite this article: W Lavrijsen 2015 J. Phys.: Conf. Ser. 664 062029 Recent citations - Giving pandas ROOT to chew on:

More information

Tokens, Expressions and Control Structures

Tokens, Expressions and Control Structures 3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type

More information

Templates Templates are functions or classes that are parameterized. We have already seen a few in STL:

Templates Templates are functions or classes that are parameterized. We have already seen a few in STL: Templates Templates are functions or classes that are parameterized. We have already seen a few in STL: std::vector< int > std::vector< double > std::list< std::vector< >> std::unordered_map< int, bool

More information

Explicit Conversion Operator Draft Working Paper

Explicit Conversion Operator Draft Working Paper Explicit Conversion Operator Draft Working Paper Lois Goldthwaite, Michael Wong IBM michaelw@ca.ibm.com ACCU Lois@LoisGoldthwaite.com Document number: N2223=07-0083 Date: 2007-03-11 Project: Programming

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 43 Dynamic Binding (Polymorphism): Part III Welcome to Module

More information

An Alternative to Name Injection from Templates

An Alternative to Name Injection from Templates Document Numbers: X3J16/95-0177 WG21/N0777 Date: September 26, 1995 Reply To: Bill Gibbons bgibbons@taligent.com An Alternative to Name Injection from Templates Introduction The current working paper specifies

More information

Instantiation of Template class

Instantiation of Template class Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or built-in types. Example: stack of int or stack of double

More information

Ch. 12: Operator Overloading

Ch. 12: Operator Overloading Ch. 12: Operator Overloading Operator overloading is just syntactic sugar, i.e. another way to make a function call: shift_left(42, 3); 42

More information

Lecture Notes on Programming Languages

Lecture Notes on Programming Languages Lecture Notes on Programming Languages 85 Lecture 09: Support for Object-Oriented Programming This lecture discusses how programming languages support object-oriented programming. Topics to be covered

More information

Implicit Evaluation of auto Variables and Arguments

Implicit Evaluation of auto Variables and Arguments Implicit Evaluation of auto Variables and Arguments Document number: N4035 (update of N3748) Authors: Joël Falcou University Paris XI, LRI Peter Gottschling SimuNova Herb Sutter Microsoft Date: 2013-08-30

More information

Type Inference auto for Note: Note:

Type Inference auto for Note: Note: Type Inference C++11 provides mechanisms for type inference which make the compiler deduce the types of expressions. I m starting the book with type inference because it can make your code more concise

More information

C++ Important Questions with Answers

C++ Important Questions with Answers 1. Name the operators that cannot be overloaded. sizeof,.,.*,.->, ::,? 2. What is inheritance? Inheritance is property such that a parent (or super) class passes the characteristics of itself to children

More information

CS

CS CS 1666 www.cs.pitt.edu/~nlf4/cs1666/ Programming in C++ First, some praise for C++ "It certainly has its good points. But by and large I think it s a bad language. It does a lot of things half well and

More information

AIMS Embedded Systems Programming MT 2017

AIMS Embedded Systems Programming MT 2017 AIMS Embedded Systems Programming MT 2017 Object-Oriented Programming with C++ Daniel Kroening University of Oxford, Computer Science Department Version 1.0, 2014 Outline Classes and Objects Constructors

More information

explicit class and default definitions revision of SC22/WG21/N1582 =

explicit class and default definitions revision of SC22/WG21/N1582 = Doc No: SC22/WG21/ N1702 04-0142 Project: JTC1.22.32 Date: Wednesday, September 08, 2004 Author: Francis Glassborow & Lois Goldthwaite email: francis@robinton.demon.co.uk explicit class and default definitions

More information

The Syntax of auto Declarations

The Syntax of auto Declarations 2007-08-01 Daveed Vandevoorde daveed@edg.com 1 The Issues Two proposed new uses of the keyword auto (one of which is already in the Working Paper) are raising new parsing ambiguity issues. The issues arise

More information

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s Session 2 - Classes in C++ Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) A C++ source file may contain: include directives #include

More information

Evolution of Programming Languages

Evolution of Programming Languages Evolution of Programming Languages 40's machine level raw binary 50's assembly language names for instructions and addresses very specific to each machine 60's high-level languages: Fortran, Cobol, Algol,

More information

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

Exceptions. CandC++ 7. Exceptions Templates. Throwing exceptions. Conveying information

Exceptions. CandC++ 7. Exceptions Templates. Throwing exceptions. Conveying information Exceptions CandC++ 7. Exceptions Templates Stephen Clark University of Cambridge (heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford and Bjarne Stroustrup) Michaelmas

More information

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 7. Exceptions Templates 2/1. Throwing exceptions 14 }

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 7. Exceptions Templates 2/1. Throwing exceptions 14 } Exceptions Some code (e.g. a library module) may detect an error but not know what to do about it; other code (e.g. a user module) may know how to handle it C++ provides exceptions to allow an error to

More information

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

G52CPP C++ Programming Lecture 7. Dr Jason Atkin G52CPP C++ Programming Lecture 7 Dr Jason Atkin 1 This lecture classes (and C++ structs) Member functions inline functions 2 Last lecture: predict the sizes 3 #pragma pack(1) #include struct A

More information

EL2310 Scientific Programming

EL2310 Scientific Programming (pronobis@kth.se) Overview Overview Wrap Up Introduction to Object Oriented Paradigm More on and Members Operator Overloading Last time Intro to C++ Differences between C and C++ Intro to OOP Today Object

More information

C++ without Classes. CMSC433, Fall 2001 Programming Language Technology and Paradigms. More C++ without Classes. Project 1. new/delete.

C++ without Classes. CMSC433, Fall 2001 Programming Language Technology and Paradigms. More C++ without Classes. Project 1. new/delete. CMSC433, Fall 2001 Programming Language Technology and Paradigms Adam Porter Sept. 4, 2001 C++ without Classes Don t need to say struct New libraries function overloading confusing link messages default

More information

Preliminaries. Part I

Preliminaries. Part I Part I Preliminaries Chapters 1 through 4 present an introduction to C++ that provides the basis for understanding the rest of the material in this book. This part also provides professional programmers

More information

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009 From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3 Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009 C++ Values, References, and Pointers 1 C++ Values, References, and Pointers 2

More information

Operator Dot Wording

Operator Dot Wording 2016-10-16 Operator Dot Wording Bjarne Stroustrup (bs@ms.com) Gabriel Dos Reis (gdr@microsoft.com) Abstract This is the proposed wording for allowing a user-defined operator dot (operator.()) for specifying

More information

UNIT TESTING OF C++ TEMPLATE METAPROGRAMS

UNIT TESTING OF C++ TEMPLATE METAPROGRAMS STUDIA UNIV. BABEŞ BOLYAI, INFORMATICA, Volume LV, Number 1, 2010 UNIT TESTING OF C++ TEMPLATE METAPROGRAMS ÁBEL SINKOVICS Abstract. Unit testing, a method for verifying a piece of software, is a widely

More information

Operator Overloading. Gary Powell, Doug Gregor, Jaakko Jȧ. rvi

Operator Overloading. Gary Powell, Doug Gregor, Jaakko Jȧ. rvi Operator Overloading Gary Powell, Doug Gregor, Jaakko Jȧ. rvi Project: Programming Language C++, Evolution Working Group Reply-to: Gary Powell , Doug Gregor ,

More information

Department of Computer science and Engineering Sub. Name: Object oriented programming and data structures Sub. Code: EC6301 Sem/Class: III/II-ECE Staff name: M.Kavipriya Two Mark Questions UNIT-1 1. List

More information

Digging into the GAT API

Digging into the GAT API Digging into the GAT API Comparing C, C++ and Python API s Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/~gallen/teaching Digging into the GAT API Design Principles Object orientation Derivation,

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++ CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 19 Introduction to C++ C++ C++ is an enormous language: All of C Classes and objects (kind of like Java, some crucial differences) Many

More information

1: Introduction to Object (1)

1: Introduction to Object (1) 1: Introduction to Object (1) 김동원 2003.01.20 Overview (1) The progress of abstraction Smalltalk Class & Object Interface The hidden implementation Reusing the implementation Inheritance: Reusing the interface

More information

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING 2011 C ++ Basics Review part 2 Auto pointer, templates, STL algorithms AUTO POINTER (AUTO_PTR) //Example showing a bad situation with naked pointers void MyFunction()

More information

2 ADT Programming User-defined abstract data types

2 ADT Programming User-defined abstract data types Preview 2 ADT Programming User-defined abstract data types user-defined data types in C++: classes constructors and destructors const accessor functions, and inline functions special initialization construct

More information

AN OVERVIEW OF C++ 1

AN OVERVIEW OF C++ 1 AN OVERVIEW OF C++ 1 OBJECTIVES Introduction What is object-oriented programming? Two versions of C++ C++ console I/O C++ comments Classes: A first look Some differences between C and C++ Introducing function

More information

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE? 1. Describe History of C++? The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was doing work for his Ph.D. thesis. One of the languages Stroustrup had the opportunity

More information

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example CS 311 Data Structures and Algorithms Lecture Slides Friday, September 11, 2009 continued Glenn G. Chappell

More information

Copyie Elesion from the C++11 mass. 9 Sep 2016 Pete Williamson

Copyie Elesion from the C++11 mass. 9 Sep 2016 Pete Williamson Copyie Elesion from the C++11 mass 9 Sep 2016 Pete Williamson C++ 11 is a whole new language Links to learning more: http://en.cppreference.com/w/cpp/language/copy_elision https://engdoc.corp.google.com/eng/doc/devguide/cpp/cpp11.shtml?cl=head

More information

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26 COSC 2P95 Procedural Abstraction Week 3 Brock University Brock University (Week 3) Procedural Abstraction 1 / 26 Procedural Abstraction We ve already discussed how to arrange complex sets of actions (e.g.

More information

Starting to Program in C++ (Basics & I/O)

Starting to Program in C++ (Basics & I/O) Copyright by Bruce A. Draper. 2017, All Rights Reserved. Starting to Program in C++ (Basics & I/O) On Tuesday of this week, we started learning C++ by example. We gave you both the Complex class code and

More information

Lecture Topics. Administrivia

Lecture Topics. Administrivia ECE498SL Lec. Notes L8PA Lecture Topics overloading pitfalls of overloading & conversions matching an overloaded call miscellany new & delete variable declarations extensibility: philosophy vs. reality

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak! //Example showing a bad situation with naked pointers CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING 2011 C ++ Basics Review part 2 Auto pointer, templates, STL algorithms void MyFunction() MyClass* ptr( new

More information

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011 CS152: Programming Languages Lecture 23 Advanced Concepts in Object-Oriented Programming Dan Grossman Spring 2011 So far... The difference between OOP and records of functions with shared private state

More information

Proposal to Simplify pair (rev 4)

Proposal to Simplify pair (rev 4) Contents Doc No: N3024=10-0014 Date: 2010-02-15 Authors: Pablo Halpern Intel Corp.. phalpern@halpernwightsoftware.com Proposal to Simplify pair (rev 4) Background... 1 Changes from N2981... 2 Changes from

More information

History C++ Design Goals. How successful? Significant constraints. Overview of C++

History C++ Design Goals. How successful? Significant constraints. Overview of C++ 1 CS 242 History C++ John Mitchell C++ is an object-oriented extension of C C was designed by Dennis Ritchie at Bell Labs used to write Unix based on BCPL C++ designed by Bjarne Stroustrup at Bell Labs

More information

Overload Resolution. Ansel Sermersheim & Barbara Geller Amsterdam C++ Group March 2019

Overload Resolution. Ansel Sermersheim & Barbara Geller Amsterdam C++ Group March 2019 Ansel Sermersheim & Barbara Geller Amsterdam C++ Group March 2019 1 Introduction Prologue Definition of Function Overloading Determining which Overload to call How Works Standard Conversion Sequences Examples

More information

Chapter 1 Getting Started

Chapter 1 Getting Started Chapter 1 Getting Started The C# class Just like all object oriented programming languages, C# supports the concept of a class. A class is a little like a data structure in that it aggregates different

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 21 November 14, 2018 CPSC 427, Lecture 21, November 14, 2018 1/23 Singleton Design Pattern (revisited) More on Functions Casts and Conversions

More information

CS93SI Handout 04 Spring 2006 Apr Review Answers

CS93SI Handout 04 Spring 2006 Apr Review Answers CS93SI Handout 04 Spring 2006 Apr 6 2006 Review Answers I promised answers to these questions, so here they are. I'll probably readdress the most important of these over and over again, so it's not terribly

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 31 Static Members Welcome to Module 16 of Programming in C++.

More information

Lambda Correctness and Usability Issues

Lambda Correctness and Usability Issues Doc No: WG21 N3424 =.16 12-0114 Date: 2012-09-23 Reply to: Herb Sutter (hsutter@microsoft.com) Subgroup: EWG Evolution Lambda Correctness and Usability Issues Herb Sutter Lambda functions are a hit they

More information

An Introduction to C++

An Introduction to C++ An Introduction to C++ Introduction to C++ C++ classes C++ class details To create a complex type in C In the.h file Define structs to store data Declare function prototypes The.h file serves as the interface

More information

Template deduction for nested classes P0293R0

Template deduction for nested classes P0293R0 Template deduction for nested classes P0293R0 S. Davis Herring October 14, 2016 Audience: EWG 1 Abstract Because type names nested inside a dependent type may be typedef-names, template deduction is not

More information

Generalized Constant Expressions

Generalized Constant Expressions Doc. no. Date: September 21, 2003 Reply-To: Gabriel Dos Reis gdr@acm.org Abstract We suggest to generalize the notion of constant expressions to include calls to constant-valued functions. The purpose

More information

Cpt S 122 Data Structures. Introduction to C++ Part II

Cpt S 122 Data Structures. Introduction to C++ Part II Cpt S 122 Data Structures Introduction to C++ Part II Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Objectives Defining class with a member function

More information

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

Expansion statements. Version history. Introduction. Basic usage

Expansion statements. Version history. Introduction. Basic usage Expansion statements Version history Document: P1306R0 Revises: P0589R0 Date: 08 10 2018 Audience: EWG Authors: Andrew Sutton (asutton@uakron.edu) Sam Goodrick (sgoodrick@lock3software.com) Daveed Vandevoorde

More information

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2. Comparison of Programming Paradigms 3. Basic Object Oriented Programming

More information

Object-Oriented Programming, Iouliia Skliarova

Object-Oriented Programming, Iouliia Skliarova Object-Oriented Programming, Iouliia Skliarova CBook a = CBook("C++", 2014); CBook b = CBook("Physics", 1960); a.display(); b.display(); void CBook::Display() cout

More information

An Incomplete Language Feature

An Incomplete Language Feature N3139=10-0129 Bjarne Stroustrup 9/4/2010 An Incomplete Language Feature Abstract As the draft standard stands, we cannot use -style initialization in default arguments. This paper argues that this is a

More information

Pairing off iterators

Pairing off iterators Pairing off iterators Anthony Williams 8th May 2002 1 Introduction Recently, a colleague approached me with an interesting problem; he had two containers with corresponding elements, so the n-th entry

More information

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I Polynomial Class Polynomial(); Polynomial(const string& N, const vector& C); Polynomial operator+(const Polynomial& RHS) const; Polynomial operator-(const Polynomial& RHS) const; Polynomial operator*(const

More information

6.001 Notes: Section 17.5

6.001 Notes: Section 17.5 6.001 Notes: Section 17.5 Slide 17.5.1 Now, let's look at one example in which changing the evaluation model allows us to explore a very different kind of computational problem. Our goal is to show how

More information