Skip to content

Commit

Permalink
Only use std::as_const to capture by const ref in the immediately inv…
Browse files Browse the repository at this point in the history
…oked (TODO nonescaping) case. Make return value of ceto::default_capture const for the other cases.
  • Loading branch information
ehren committed Aug 22, 2024
1 parent 3a4e1f0 commit 82ecf2f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ceto/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ def is_capture(n):
capture_list = ["&" + i + " = " + "std::as_const(" + i + ")" for i in possible_captures]
else:
# capture only a few things by const value (shared/weak instances, arithithmetic_v, enums):
capture_list = [i + " = " + "std::as_const(ceto::default_capture(" + i + "))" for i in possible_captures]
capture_list = [i + " = " + "ceto::default_capture(" + i + ")" for i in possible_captures]
else:
capture_list = ""

Expand Down
6 changes: 3 additions & 3 deletions include/ceto.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,19 +290,19 @@ call_or_construct(TArgs&&... args) {

// this one may be controversial (strong capture of shared object references by default - use 'weak' to break cycle)
template <class T>
std::enable_if_t<std::is_base_of_v<object, T>, std::shared_ptr<T>> // We now autoderef all shared/unique_ptrs not just to ceto class instances. doing the same for lambda capture might go a bit too far - don't want to encourange writing shared_ptr<vector<int>> instead of creating a wrapper class instance that's automatically placed in the capture list
std::enable_if_t<std::is_base_of_v<object, T>, const std::shared_ptr<T>> // We now autoderef all shared/unique_ptrs not just to ceto class instances. doing the same for lambda capture might go a bit too far - don't want to encourange writing shared_ptr<vector<int>> instead of creating a wrapper class instance that's automatically placed in the capture list
constexpr default_capture(std::shared_ptr<T> t) {
return t;
}

template <class T>
std::enable_if_t<std::is_base_of_v<object, T>, std::weak_ptr<T>>
std::enable_if_t<std::is_base_of_v<object, T>, const std::weak_ptr<T>>
constexpr default_capture(std::weak_ptr<T> t) {
return t;
}

template <class T>
std::enable_if_t<std::is_arithmetic_v<T> || std::is_enum_v<T>, T>
std::enable_if_t<std::is_arithmetic_v<T> || std::is_enum_v<T>, const T>
constexpr default_capture(T t) {
return t;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/regression/capture.donotedit.autogenerated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ auto foo(const T1& x) const -> void {
};
l();
const auto i = std::make_shared<const decltype(Inner{f})>(f);
[&]() {
[&x = std::as_const(x), &i = std::as_const(i)]() {
if constexpr (!std::is_void_v<decltype((*ceto::mad(i)).foo(x))>) { return (*ceto::mad(i)).foo(x); } else { static_cast<void>((*ceto::mad(i)).foo(x)); };
}();
}
Expand Down

0 comments on commit 82ecf2f

Please sign in to comment.