Say we have this header file:
#pragma once
#include <vector>
class MyClass
{
public:
MyClass(double);
/* ... */
private:
std::vector<double> internal_values;
};
Run Code Online (Sandbox Code Playgroud)
Now, whenever we use #include "MyClass.hpp" in some other hpp or cpp file, we effectively also #include <vector>, despite the fact that we do not need it. The reason I am saying it is not needed is that std::vector is only used internally in MyClass, but it is not required at all to actually interact …
是否有一种安全的标准方法可以转换std::string_view为int?
由于 C++11std::string允许我们使用stoi转换为int:
std::string str = "12345";
int i1 = stoi(str); // Works, have i1 = 12345
int i2 = stoi(str.substr(1,2)); // Works, have i2 = 23
try {
int i3 = stoi(std::string("abc"));
}
catch(const std::exception& e) {
std::cout << e.what() << std::endl; // Correctly throws 'invalid stoi argument'
}
Run Code Online (Sandbox Code Playgroud)
但stoi不支持std::string_view。因此,或者,我们可以使用atoi,但必须非常小心,例如:
std::string_view sv = "12345";
int i1 = atoi(sv.data()); // Works, have …Run Code Online (Sandbox Code Playgroud) 我知道使用C++可以创建包含某些功能的DLL文件,然后可以将其导入Excel(例如通过VBA).我们来看看下面的C++函数
double __stdcall square_it(double &x)
{
return x*x;
}
Run Code Online (Sandbox Code Playgroud)
我们假设它包含在square.dll中,以便我们可以使用以下VBA导入
Declare PtrSafe Function square_it Lib "square.dll" (ByRef x As Double) As Double
Private Sub TestSub()
MsgBox square_it(4.5)
End Sub
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:是否可以在Scala中编写函数,然后以类似的方式从VBA调用它?
是否可以将fmt::Display和fmt::Debug一起定义,即它们使用相同的实现?
假设我们有以下示例代码(纯粹用于演示目的):
\nuse std::fmt;\n\nstruct MyDate {\n pub year: u16,\n pub month: u8,\n pub day: u8\n}\n\nimpl PartialEq for MyDate {\n fn eq(&self, other: &Self) -> bool {\n self.year == other.year && self.month == other.month && self.day == other.day\n }\n}\n\nimpl fmt::Display for MyDate {\n fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {\n write!(f, "{}-{:02}-{:02}", self.year, self.month, self.day)\n }\n}\n\nimpl fmt::Debug for MyDate {\n fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {\n write!(f, "{}", self)\n }\n}\n\nfn main() {\n …Run Code Online (Sandbox Code Playgroud) 我尝试使用该特征“重载”函数From(以便它可以接受结构和字符串):
pub struct Measurement {
pub value: i16,
pub unit: char,
}
impl From<&str> for Measurement {
fn from(s: &str) -> Measurement {
let value = s[0..s.len() - 1].parse::<i16>().unwrap();
let unit = s.chars().last().unwrap();
return Measurement { value, unit };
}
}
pub fn print_measurement<T: Into<Measurement>>(value: T) {
let m = value.into();
println!("Measurement is {}{}", m.value, m.unit);
}
fn main() {
print_measurement("40m");
print_measurement(Measurement{value: 23, unit: 'g'});
}
Run Code Online (Sandbox Code Playgroud)
根据Playground,这按预期工作。但是,由于解析字符串可能会失败,因此我想使用try_into(), 而不是into()。所以:
use std::convert::TryFrom;
#[derive(Debug)]
pub …Run Code Online (Sandbox Code Playgroud) 基于如何判断模板类型是否是模板类的实例?并检查类是否是模板专业化?我创建了以下变体来检查MyClass1, MyClass2or 的特定实例MyClass3:
template <class T, template <class...> class Template>
constexpr bool is_instance_of_v = false;
template <template <class...> class Template, class... Args>
constexpr bool is_instance_of_v<Template<Args...>, Template> = true;
template<class T> struct MyClass1 { };
template<class T, class B> struct MyClass2 { };
template<class T, bool B> struct MyClass3 { };
int main(int argc, char* argv[])
{
constexpr bool b1 = is_instance_of_v<MyClass1<float>, MyClass1>;
constexpr bool b2 = is_instance_of_v<MyClass1<float>, MyClass2>;
// constexpr bool …Run Code Online (Sandbox Code Playgroud) 我有以下设置(使用g++10 和lcov1.14):
g++ SampleScript.cpp -g -O0 -fprofile-arcs -ftest-coverage -o MyScript
./MyScript
lcov -d . -c -o coverage.info --rc lcov_branch_coverage=1
genhtml coverage.info -o cov_out --legend --branch-coverage
Run Code Online (Sandbox Code Playgroud)
和
/* SampleScript.cpp */
class Container
{
public:
Container()
: m_value(0) { }
Container(int value)
: m_value(value) { }
int& value()
{
return m_value;
}
const int& value() const
{
return m_value;
}
private:
int m_value;
};
int main()
{
const Container c;
return c.value();
}
Run Code Online (Sandbox Code Playgroud)
但即使我的代码跳过了 2 个函数(1 个构造函数和 1 …
让我们考虑一下这段代码:
int main()
{
int a = 1;
auto f1 = [a]() {
int a = 10;
return a;
};
auto f2 = []() {
int a = 100;
return a;
};
return a + f1() + f2();
}
Run Code Online (Sandbox Code Playgroud)
将 flag-Wshadow与 gcc 一起使用(在 10.2 上测试)时,我们收到以下警告:
<source>:26:13: warning: declaration of 'a' shadows a lambda capture [-Wshadow]
6 | int a = 10;
<source>:21:13: warning: declaration of 'a' shadows a previous local [-Wshadow]
11 | int a = 100;
Run Code Online (Sandbox Code Playgroud)
我理解第一种情况,我们明确捕获 …
我正在使用遗留库的项目上工作,该遗留库使用函数定义如
void func() throw(some_exception);
Run Code Online (Sandbox Code Playgroud)
由于动态异常规范在C ++ 17中已删除,所以我想知道如何解决此问题。
P0003R0建议更换
void func() throw(some_exception)
{
/* body */
}
Run Code Online (Sandbox Code Playgroud)
用类似的东西
void func()
{
try
{
/* body */
}
catch(const some_exception&) {
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我无权访问源代码(仅头文件)。
因此,我只能尝试“修复”标头中的函数定义。所以例如我可以写
void func() noexcept(false);
Run Code Online (Sandbox Code Playgroud)
但是,当函数引发异常时,我的应用程序仍会终止。
如何更改头文件中的函数定义或可能调整我自己的项目(我使用的地方func)以获得与throw(some_exception)C ++ 17之前相同的行为?
这篇文章提供了一个非常简洁且纯 C++ 的算法,用于将序列日期 (Excel) 转换为其显式的年月日表示形式(以及反之)。为了方便起见,我贴一个压缩版本:
void ExcelSerialDateToDMY(int nSerialDate, int& nDay, int& nMonth, int& nYear)
{
// Modified Julian to DMY calculation with an addition of 2415019
int l = nSerialDate + 68569 + 2415019;
int n = int(( 4 * l ) / 146097);
l = l - int(( 146097 * n + 3 ) / 4);
int i = int(( 4000 * ( l + 1 ) ) / 1461001);
l = l - int(( 1461 * …Run Code Online (Sandbox Code Playgroud)