Name | Ownership | Copyable | Movable | Sharing | Lifetime | Semantics |
---|---|---|---|---|---|---|
unique_ptr | Unique | ❌ | ✅ | ❌ | Lexical | Reference |
shared_ptr | Shared | ✅ | ✅ | Reference | Reference-counted | Reference |
weak_ptr | ❌ | ✅ | ✅ | ❌ | Non-extending | Optional-reference |
value_ptr | Unique | ✅ | ✅ | Value | Lexical | Value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constexpr int square(int x) { | |
return x * x; | |
} | |
int main() { | |
return square(4); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main: # @main | |
mov eax, 16 | |
ret |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<unsigned n> | |
struct Arg { | |
template<class X, class...Xs> | |
constexpr auto operator()(X x, Xs...xs) { | |
return Arg<n - 1>{}(xs...); | |
} | |
}; | |
template<> | |
struct Arg<0> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<unsigned n> | |
struct Get { | |
template<class X, class…Xs> | |
constexpr auto operator()(X x, Xs…xs) { | |
if constexpr(n > sizeof…(xs) ) { | |
return; | |
} else if constexpr(n > 0) { | |
return Get<n-1>{}(xs…); | |
} else { | |
return x; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<class T> | |
constexpr auto supportsAPI(T x) -> decltype(x.Method1(), x.Method2(), true_type{}) { | |
return {}; | |
} | |
constexpr auto supportsAPI(...) -> false_type { | |
return {}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<class T> | |
auto compute(T x) -> decltype( enable_if_t< supportsAPI(T{}), int>{}) { | |
return x.Method(); | |
} | |
template<class T> | |
auto compute(T x) -> decltype( enable_if_t<!supportsAPI(T{}), int>{}) { | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<class T> | |
int compute(T x) { | |
if constexpr( supportsAPI(T{}) ) { | |
// only gets compiled if the condition is true | |
return x.Method(); | |
} else { | |
return 0; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct BazTag : FooTag, BarTag {}; | |
template<class L, class R, | |
enable_if_t< | |
is_same<L::tag, FooTag>::value && | |
is_base_of<R::tag, BarTag>::value | |
> fold(L l, R r) { | |
return foldFB(l, r); | |
} |