std::nullptr_t & std::void_t

std::nullptr_t

std::nullptr_tnullptr的模板类型,用法如下:

#include <iostream>
#include <type_traits>

void foo(int *) {
    std::cout << "int*\n";
}

void foo(double *) {
    std::cout << "double*\n";
}

void foo(std::nullptr_t) {
    std::cout << "nullptr\n";
}


int main() {
    int *a = nullptr;
    double *b = nullptr;
    foo(a);
    foo(b);
    foo(nullptr);  // (1)
    return 0;
}
/*
int*
double*
nullptr
*/

(1)中,如果没有 std::nullptr_tnullptr 不知道应该匹配int*还是double*

std::void_t

该模板的定义如下:

template< class... >
using void_t = void;

看起来没啥卵用,这个需要和SFINAE结合使用。举个例子,判定传入的结构是否包含tp 类型:

#include <iostream>
#include <utility>

// 泛型模板
template<typename T, typename = void>
struct HasTypeFoo : std::false_type {
};

// 模板特化,T::tp 特化的模板
template<typename T>
struct HasTypeFoo<T, std::void_t<typename T::tp>> : std::true_type {
};

struct Foo {
    using tp = int;
};

struct FooFake {
    using tp1 = double;
};

struct SubFoo : Foo {
    using tp2 = float;
};

int main() {
    std::cout << HasTypeFoo<Foo>::value << std::endl;
    std::cout << HasTypeFoo<FooFake>::value << std::endl;
    std::cout << HasTypeFoo<SubFoo>::value << std::endl;
    return 0;
}

上面的代码中,HasTypeFoo 是泛型模板。如果泛型Ttp 类型成员,那么会特化成std::void_t 类型;否则匹配失败,SFINAE 规则会自动匹配typename = void 的候选类型。
这里的代码中,std::void_t 的左右就是为了模板特化。因为typename = void 默认模板,总是最后才会执行匹配。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<iostream> #include<ctime> #include<chrono> #include<string> #include<filesystem> #include<fstream> #include<sstream> #include<thread> #include<boost/filesystem.hpp> const uintmax_t MAX_LOGS_SIZE = 10ull * 1024ull * 1024ull * 1024ull; //const uintmax_t MAX_LOGS_SIZE = 10ull; void create_folder(std::string folder_name) { boost::filesystem::create_directory(folder_name); std::string sub_foldername=folder_name+"/logs_ros"; boost::filesystem::create_directory(sub_foldername); } std::string get_current_time() { auto now = std::chrono::system_clock::now(); std::time_t now_c = std::chrono::system_clock::to_time_t(now); std::tm parts = *std::localtime(&now_c); char buffer[20]; std::strftime(buffer, sizeof(buffer), "%Y-%m-%d-%H-%M", &parts); return buffer; } void check_logs_size() { std::string logs_path = "/home/sage/logs/"; boost::filesystem::path logs_dir(logs_path); std::uintmax_t total_size = 0; for (const auto& file : boost::filesystem::recursive_directory_iterator(logs_dir)) { if (boost::filesystem::is_regular_file(file)) { total_size += boost::filesystem::file_size(file); } } if (total_size > MAX_LOGS_SIZE) { boost::filesystem::path earliest_dir; std::time_t earliest_time = std::time(nullptr); for (const auto& dir : boost::filesystem::directory_iterator(logs_dir)) { if (boost::filesystem::is_directory(dir)) { std::string dir_name = dir.path().filename().string(); std::tm time_parts = {}; std::istringstream ss(dir_name); std::string part; std::getline(ss, part, '-'); time_parts.tm_year = std::stoi(part) - 1900; std::getline(ss, part, '-'); time_parts.tm_mon = std::stoi(part) - 1; std::getline(ss, part, '-'); time_parts.tm_mday = std::stoi(part); std::getline(ss, part, '-'); time_parts.tm_hour = std::stoi(part); std::getline(ss, part, '-'); time_parts.tm_min = std::stoi(part); std::time_t dir_time = std::mktime(&time_parts); if (dir_time < earliest_time) { earliest_time = dir_time; earliest_dir = dir.path(); } } } if (!earliest_dir.empty()) { boost::filesystem::remove_all(earliest_dir); } } } int main() { std::string logs_path = "/home/sage/logs/"; while (true) { std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); std::time_t now_c = std::chrono::system_clock::to_time_t(now); std::tm parts = *std::localtime(&now_c); if (parts.tm_min % 10 == 0) { std::string folder_name = logs_path + get_current_time(); create_folder(folder_name); } check_logs_size(); std::this_thread::sleep_for(std::chrono::minutes(1)); } return 0; }修改为ros节点
06-09

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值