Quick actions

cmd+k|ctrl+k

Navigation

Languages

Badge

Snippet info

Language

Cpp

Visibility

public

Author

steinwaywhw

Created

2019-06-27T02:07:32Z

Updated

2019-06-27T02:07:32Z

// The Badge can only be constructed by T.
template <typename T>
class Badge{
 private:
  friend T;
  Badge() {}
};

// RestrictedFunc can only be called with a AllowedCaller Badge.
class AllowedCaller;
void RestrictedFunc(Badge<AllowedCaller>);

// AllowedCaller calls the RestrictedFunc with a Badge constructed using {}.
class AllowedCaller {
 public:
  void CallSite() {
    RestrictedFunc({});
  }
};
void RestrictedFunc(Badge<AllowedCaller>) {};

// DisallowedCaller will not be able to construct the Badge with {}.
// class DisallowedCaller {
// public:
//  void CallSite() {
//   RestrictedFunc({});
//  }
// };

int main() {
    AllowedCaller caller;
    caller.CallSite();
    return 0;
}
INFO