Skip to content

Commit

Permalink
update lambda.cc & type_name.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Dec 16, 2024
1 parent 6eaa8de commit d3a4c01
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
42 changes: 22 additions & 20 deletions tests/lambda.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
// Created by Hedzr Yeh on 2021/10/14.
//

#include "undo_cxx.hh"
#include "undo_cxx/undo-def.hh"

#include <cmath>
#include <iostream>
#include <math.h>
#include <string>

#include <functional>
#include <memory>
#include <random>

#include <mutex>
#include <vector>

namespace lambdas {
void f(int n1, int n2, int n3, const int &n4, int n5) {
static void f(int n1, int n2, int n3, const int &n4, int n5) {
std::cout << n1 << ' ' << n2 << ' ' << n3 << ' ' << n4 << ' ' << n5 << '\n';
}

int g(int n1) { return n1; }
static int g(int n1) { return n1; }

struct Foo {
void print_sum(int n1, int n2) {
Expand All @@ -34,14 +34,14 @@ namespace lambdas {
int data = 10;
};

void test_lambdas() {
static void test_lambdas() {
{
using namespace std::placeholders;
std::vector<std::function<int(int)>> functions;

std::function<int(int, int)> foo = [](int a, int b) { return a + b; };
std::function<int(int)> bar = std::bind(foo, 2, std::placeholders::_1);
std::function<int(int, int)> bar2args = std::bind(foo, _1, _2);
std::function<int(int, int)> const foo = [](int a, int b) { return a + b; };
std::function<int(int)> const bar = std::bind(foo, 2, std::placeholders::_1);
std::function<int(int, int)> const bar2args = std::bind(foo, _1, _2);

functions.push_back(bar);
UNUSED(bar2args);
Expand All @@ -64,9 +64,9 @@ namespace lambdas {
f2(10, 11, 12); // 进行到 f(12, g(12), 12, 4, 5); 的调用

// 常见使用情况:以分布绑定 RNG
std::default_random_engine e;
std::uniform_int_distribution<> d(0, 10);
std::function<int()> rnd = std::bind(d, e); // e 的一个副本存储于 rnd
std::default_random_engine const e;
std::uniform_int_distribution<> const d(0, 10);
std::function<int()> const rnd = std::bind(d, e); // e 的一个副本存储于 rnd
for (int n1 = 0; n1 < 10; ++n1)
std::cout << rnd() << ' ';
std::cout << '\n';
Expand All @@ -87,18 +87,20 @@ namespace lambdas {
}
} // namespace lambdas

void test_lock_guard() {
{
std::mutex mu;
mu.unlock();
std::lock_guard<std::mutex> lock(mu);
namespace {

static void test_lock_guard() {
{
std::mutex mu;
mu.unlock();
std::lock_guard<std::mutex> const lock(mu);
}
}
}

int main() {
} // namespace

int main() {
test_lock_guard();
lambdas::test_lambdas();

return 0;
}
12 changes: 6 additions & 6 deletions tests/type-name.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
// Created by Hedzr Yeh on 2021/10/16.
//

#include <cstddef>
#include <iostream>
#include <list>
#include <stack>
#include <string>
#include <string_view>
#include <vector>

template<typename T>
constexpr std::string_view type_name();
constexpr static std::string_view type_name();

template<>
constexpr std::string_view type_name<void>() { return "void"; }
Expand All @@ -25,7 +25,7 @@ namespace detail {
using type_name_prober = void;

template<typename T>
constexpr std::string_view wrapped_type_name() {
constexpr static std::string_view wrapped_type_name() {
#ifdef __clang__
return __PRETTY_FUNCTION__;
#elif defined(__GNUC__)
Expand All @@ -37,11 +37,11 @@ namespace detail {
#endif
}

constexpr std::size_t wrapped_type_name_prefix_length() {
constexpr static std::size_t wrapped_type_name_prefix_length() {
return wrapped_type_name<type_name_prober>().find(type_name<type_name_prober>());
}

constexpr std::size_t wrapped_type_name_suffix_length() {
constexpr static std::size_t wrapped_type_name_suffix_length() {
return wrapped_type_name<type_name_prober>().length() - wrapped_type_name_prefix_length() - type_name<type_name_prober>().length();
}

Expand Down

0 comments on commit d3a4c01

Please sign in to comment.