变长参数在fmt里的使用,以及一些展开的写法

 注意:fmt::format内部调用的就是fmt::format_to

demo:

#include <fmt/core.h>
#include <fmt/args.h>

void fun()
{
    double const d = 123.45678;
    int x = 10;
    fmt::memory_buffer buf;
    fmt::format_to(std::back_inserter(buf), "{:e},{}", d, x);
    auto info = to_string(buf);  // "1.234568e+02,10"
}

 fmt::format_to的定义:

template <typename OutputIt, typename... T,
          FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, char>::value)>
FMT_INLINE auto format_to(OutputIt out, format_string<T...> fmt, T&&... args)
    -> OutputIt {
  return vformat_to(out, fmt, fmt::make_format_args(args...));
}


//format_string的定义
//char是字符类型,这里传进来的Args...是double const&
template <typename... Args>
using format_string = basic_format_string<char, type_identity_t<Args>...>;

仔细观察format_to这个函数,基本上只有两个参数类型,一个是OutputIt,一个是...T

在demo里用的时候,没有显式给出变量类型,而是纯由编译器来推导

fmt::format_to(std::back_inserter(buf), "{:e},{}", d, x);

很神奇,怎么推导出来的呢。

第一个肯定就推导成OutputIt了。

第二个参数fmt的时候,fmt按照format_string<T...>的样式进行推导,此时要确定T...,那么这个T...就是下面的两个参数const double和int了,由于这里用的是模板,而且用的是T&&,所以折叠后就变成了const double&和int&了

第三个和第四个参数,就属于T...了

看basic_format_string的定义。

template <typename... Args>
using format_string = basic_format_string<char, type_identity_t<Args>...>;

format_string的定义中,第一个参数是字符类型char,剩下的是type_identity_t<Args>...

一定要注意它的这个写法,看type_identity的定义,这个原函数的值type就是传给它的T:

template <typename T> struct type_identity { using type = T; };
template <typename T> using type_identity_t = typename type_identity<T>::type;

type_identity_t<Args>...最后的...表示要把变长参数进行展开,也就是:

type_identity_t<Args_0>, type_identity_t<Args_1>, type_identity_t<Args_2>, ...

也就是:

type_identity<const double&>::type, type_identity<int&>::type

所以basic_format_string实例化后的结果就是:

basic_format_string<char,double const &,int &> 


template <typename... Args>
using format_string = basic_format_string<char, type_identity_t<Args>...>;

推导的结果:

拷贝出来,如下:

fmt::v8::format_to<std::back_insert_iterator<fmt::v8::basic_memory_buffer<char,500,std::allocator<char> > >,double const &,int &,0>

(

std::back_insert_iterator<fmt::v8::basic_memory_buffer<char,500,std::allocator<char> > > out={...},

fmt::v8::basic_format_string<char,double const &,int &> fmt,
const double & <args_0>,

int & <args_1>

) 

basic_format_string在初始化的时候,

注意下面也牵扯到展开,而且这种更不容易看懂:

detail::count<
            (std::is_base_of<detail::view, remove_reference_t<Args>>::value &&
             std::is_reference<Args>::value)...>() == 0
void fun()
{
        double const d = 123.45678;
        int x = 10;
        fmt::memory_buffer buf;
        //fmt::format_to(std::back_inserter(buf), "{:e},{}", d, x);
        auto arg0 = fmt::arg("x", 1);
        fmt::format_to(std::back_inserter(buf), "{:e},{x}", d, arg0);
        
        auto info = to_string(buf);  // "1.234568e+02,10"
}

 这里的count函数,是记录view的子类引用的个数的,如果不为0,那么就编译报错。

template <bool B = false> constexpr auto count() -> size_t { return B ? 1 : 0; }
template <bool B1, bool B2, bool... Tail> constexpr auto count() -> size_t {
  return (B1 ? 1 : 0) + count<B2, Tail...>();
}

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值