任何人都可以解释在类中定义的静态函数和在file.hpp中声明并在file.cpp中定义的静态函数之间的区别(我只能在此文件中使用此静态函数?
考虑以下代码:
#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) 我试图了解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,则选择部分模板特化,因此如果选择:
我想将数字 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