Skip to content

Latest commit

 

History

History
53 lines (36 loc) · 1.15 KB

371.md

File metadata and controls

53 lines (36 loc) · 1.15 KB
Info

Example

void newapi();
void oldapi() = delete("This old API is outdated and already been removed. Please use newapi() instead.");

int main () {
  oldapi();
}

https://godbolt.org/z/aPz1bM4x6

Puzzle

  • Can you implment simplified addressof with delete("Cannot take address of rvalue.") when used incorreclty?
// TODO addressof

int main() {
  int i{};
  addressof(i);     // okay
  addressof(int{}); // error: "Cannot take address of rvalue."
}

https://godbolt.org/z/M6881vY65

Solutions

template<class T> constexpr T* addressof(T& r) noexcept { return &r; }
template<class T> const T* addressof(const T&&) = delete("Cannot take address of rvalue.");

int main() {
  int i{};
  addressof(i);     // okay
  addressof(int{}); // error: "Cannot take address of rvalue."
}

https://godbolt.org/z/hMYWThGxj