[PATCH] D66148: [SemanticTypedef] Provide a semantic typedef class and operators

David Greene via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 13 09:42:43 PDT 2019


greened created this revision.
greened added reviewers: jdoerfert, rnk, nliber.
greened added a project: LLVM.
Herald added subscribers: llvm-commits, dexonsmith, mgorny.
greened edited the summary of this revision.

A semanic typedef is a kind of "strong typedef" that prevents implicit
conversion between itself and its underlying type.  But rather than a
straight typedef of the base type with all operators available, a
semantic typedef allows the new type to choose the operators available
to it, increasing type safety.

An example:

  // It doesn't make sense to add two CacheLineSize objects together, so
  // don't define add-assignment.
  struct CacheLineSize : public SemanticTypedef<CacheLineSize, unsigned>,
                       : public EqualityCompare<CacheLineSize>,
    		       : public LessThanCompare<CacheLineSize> {
    using SemanticTypedef::SemanticTypedef;
  };
  
  CacheLineSize ASize(32);
  CacheLineSize BSize(64);
  
  assert(ASize < BSize);
  
  static_assert(!std::is_convertible<decltype(ASize),
                                     UnderlyingType<decltype(ASize)>>::value,
                "Converted CacheLineSize to its underlying type!");
  
  static_assert(!std::is_convertible<UnderlyingType<decltype(ASize)>,
                                     decltype(ASize)>::value,
                "Converted underlying type to CacheLineSize!");

This commit defines the basic SemanticTypedef class along with classes
to make the most common operators available: ==, <, >=, =, +=, &&, ||,
!, << (output streaming).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D66148

Files:
  llvm/include/llvm/ADT/SemanticTypedef.h
  llvm/unittests/ADT/CMakeLists.txt
  llvm/unittests/ADT/SemanticTypedef.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66148.214858.patch
Type: text/x-patch
Size: 19766 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190813/84482fe4/attachment.bin>


More information about the llvm-commits mailing list