Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements for lambda inputs. #369

Open
Yurlungur opened this issue Apr 25, 2024 · 0 comments
Open

Enhancements for lambda inputs. #369

Yurlungur opened this issue Apr 25, 2024 · 0 comments
Assignees
Labels
clean-up Changes that don't affect code execution but make the code cleaner enhancement New feature or request

Comments

@Yurlungur
Copy link
Collaborator

When we switch to C++17 we should use the following model to allow us to pass nullptr into functions for lambda directly. We should also consider switching to std::optional.

template <typename T>
struct remove_cvref {
  typedef std::remove_cv_t<std::remove_reference_t<T>> type;
};
template <typename T>
using remove_cvref_t = typename remove_cvref<T>::type;

template<typename T>
constexpr inline bool is_null(T &&t) {
    if constexpr(std::is_pointer<remove_cvref_t<T>>::value) {
        return std::forward<T>(t) == nullptr;
    } else {
        return false;
    }
}

#define SG_DO_IF_NOT_NULL(lambda) \
  if constexpr (!std::is_null_pointer<remove_cvref_t<decltype(lambda)>>::value) \
    if (!is_null(lambda)) 

template<typename T>
void DoSomething(T &&t) {
    SG_DO_IF_NOT_NULL(t) {
        std:: cout << t[0] << std::endl;
    }
}

int main() {

    auto q = nullptr;
    std::vector<Real> v = {1, 2, 3, 4};
    DoSomething(v);
    DoSomething(q);

    return 0;
}
@Yurlungur Yurlungur added enhancement New feature or request clean-up Changes that don't affect code execution but make the code cleaner labels Apr 25, 2024
@Yurlungur Yurlungur self-assigned this Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clean-up Changes that don't affect code execution but make the code cleaner enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant