小编bla*_*zio的帖子

C++中静态函数之间的区别

任何人都可以解释在类中定义的静态函数和在file.hpp中声明并在file.cpp中定义的静态函数之间的区别(我只能在此文件中使用此静态函数?

c++ static function

3
推荐指数
1
解决办法
1330
查看次数

为什么在这种情况下不会出现复制省略?

考虑以下代码:

#include <iostream>

struct S
{
    S(std::string s) : s_{s} { std::cout << "S( string ) c-tor\n"; }
    S(S const&) { std::cout << "S( S const& ) c-tor\n"; }
    S(S&& s) { std::cout << "S&& c-tor\n"; s_ = std::move(s.s_); }
    S& operator=(S const&) { std::cout << "operator S( const& ) c-tor\n";  return *this;}
    S& operator=(S&& s) { std::cout << "operator (S&&)\n"; s_ = std::move(s.s_); return *this; }
    ~S() { std::cout << "~S() d-tor\n"; }

    std::string s_;
};

S foo() { …
Run Code Online (Sandbox Code Playgroud)

c++ copy-elision

3
推荐指数
1
解决办法
84
查看次数

当通过参数启用时,std :: enabled_if如何工作

我试图了解enable_if是如何工作的,除了方案#3之外,我几乎理解了所有内容

https://en.cppreference.com/w/cpp/types/enable_if

template<class T>
void destroy(T* t, 
         typename 
std::enable_if<std::is_trivially_destructible<T>::value>::type* = 0) 
{
    std::cout << "destroying trivially destructible T\n";
}
Run Code Online (Sandbox Code Playgroud)

如果enable_if中的表达式为true,则选择部分模板特化,因此如果选择:

  1. 为什么在enable_if只是条件而没有指示第二个模板参数?
  2. 什么类型的"类型*"呢?无效*?如果是这样,为什么?
  3. 为什么是指针?

c++ templates metaprogramming enable-if

1
推荐指数
1
解决办法
76
查看次数

提升多精度的精度舍入问题

我想将数字 123456789.123456789 乘以 1000000000.0,作为此操作的结果,我期望 123456789123456789 作为 int 或 float 123456789123456789.0,但我得到:

res: 123456789123456791.04328155517578125
int_res: 123456789123456791
Run Code Online (Sandbox Code Playgroud)

我应该用其他方式来做吗?

#include <iostream>

#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>

namespace bmp = boost::multiprecision;

int main() 
{
    bmp::cpp_dec_float_100 scalar{1000000000.0};
    bmp::cpp_dec_float_100 a{123456789.123456789};
    bmp::cpp_dec_float_100 res = a * scalar;    
    bmp::cpp_int int_res = res.convert_to<bmp::cpp_int>();
    std::cout << "    res: " << res.str() << std::endl;
    std::cout << "int_res: " << int_res.str() << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

代码: https: //wandbox.org/permlink/xB8yBWuzzGvQugg7

c++ precision boost boost-multiprecision

1
推荐指数
1
解决办法
224
查看次数