What is the use of C++ Transparent Class Wrapper


<ol><li>What is call "Transparent Class Wrapper" in C++</li> <li>Why it is call "Transparent..."</li> <li>What is the use of it (What cannot do without 'Transparent Class Wrapper').</li> </ol>

Appreciate some conceptual explanation.


A transparent class wrapper is a wrapper around a type, where the wrapper behaves the same as the underlying type - hence "transparent".

To explain it as well as its use, here's an example where we wrap an int but overload operator++() to output a message whenever it is used (inspired by <a href="http://bytes.com/topic/c/answers/428034-transparent-class-wrapper" rel="nofollow">this thread</a>):

class IntWrapper { int data; public: IntWrapper& operator++() { std::cout << "++IntWrapper\n"; data++; return *this; } IntWrapper(int i) : data(i) {} IntWrapper& operator=(const IntWrapper& other) { data = other.data; return *this; } bool operator<(const IntWrapper& rhs) const { return data < rhs.data; } // ... other overloads ... };

We can then replace usages of int with IntWrapper if we choose to:

for (int i = 0; i < 100; ++i) { /* ... */ } // becomes for (IntWrapper i = 0; i < 100; ++i) { /* ... */ }

Except the latter will print a message whenever preincrement is called.

Note that I supplied a non-explicit constructor IntWrapper(int i). This ensures that whenever I use an int where an IntWrapper is expected (such as IntWrapper i = 0), the compiler can silently use the constructor to create an IntWrapper out of the int. The Google C++ style Guide discourages single-argument non-explicit constructors for precisely this reason, as there may be conversions where you didn't expect, which hurts type safety. On the other hand, this is exactly what you want for transparent class wrappers, because you do want the two types to be readily convertible.

That is:

// ... explicit IntWrapper(int i) ... // ... IntWrapper i = 0; // this will now cause a compile error


Most likely, you're referring to a lightweight inline (header file) wrapper class, though I'm not familiar with the term. Adding a level of abstraction like this is useful in permitting generic client code.


  • mem_fn to function of member object
  • What's the essential difference between these two variadic functions?
  • std::system Exception when instantiating a singleton object
  • Deleting a widget from QTableView
  • multidatatrigger with multibinding in ControlTemplate.Triggers
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • c++ regex_replace not doing intended substitution
  • C++ friend class std::vector
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • Build Successful but not running on simulator
  • Reduction and collapse clauses in OMP have some confusing points
  • Stop Bash Script if Hive Fails
  • C: Incompatible pointer type initializing
  • Google Custom Search with transparent background
  • Why value captured by reference in lambda is broken? [duplicate]
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • Obtain ObjectIdHex value from mgo query
  • Volusion's generic SQL folder, functionality
  • How to set/get protobuf's extension field in Go?
  • JSON with duplicate key names losing information when parsed
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • trying to dynamically update Highchart column chart but series undefined
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • embed rChart in Markdown
  • Django query for large number of relationships
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?