我有一个模板类,我想根据模板类型来切换其中的内容是否为常量。
伪代码:
template<bool isConst>
Class ConstableClass
{
public:
// if isConst == true make this method const
void DoSomething() "const";
// if isConst == true make the returned smartpointer type const method const
std::unique_ptr<"const" int> operator->() "const";
private:
// if isConst == true add const at the start of the next line
"const" int foo;
}
Run Code Online (Sandbox Code Playgroud)
这种事情可能吗?
我在使用此代码时遇到了一些问题。它在调试模式下工作,但在发布模式下会错误地为某些 csv 行输出 0.0。只有当我在第 19 行附近添加关闭优化的预处理器命令时,一切才开始在发布模式下工作。有没有人知道为什么会这样,我以前从未遇到过这种行为。
#include <vector>
#include <complex>
#include <fstream>
size_t constexpr kBins = 64;
double constexpr kFrequency = 16.0;
double Triangle(double const bin)
{
return abs(bin - floor(bin + 1.0 / 2.0));
}
int main()
{
std::vector<std::complex<double>> input{kBins, {0.0, 0.0}};
for (size_t i = 0; i < kBins; ++i)
{
#pragma optimize("" off)
double value = sin(2.0 * M_PI * kFrequency * Triangle(static_cast<double>(i) / kBins)) / (2.0 * M_PI * kFrequency * Triangle(static_cast<double>(i) / kBins));
#pragma optimize("" …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建我的第一个单元测试,并且有一个类可以分别在其构造函数和析构函数中递增和递减实例计数器。我有一个测试来确保它有效但它失败了,似乎我的其他测试中的其他类实例在超出范围时没有调用它们的析构函数。
public class Customer
{
public Customer()
{
++InstanceCount;
}
public Customer(int customerId)
{
CustomerId = customerId;
++InstanceCount;
}
~Customer()
{
--InstanceCount;
}
public static int InstanceCount { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
[TestClass]
public class CustomerTest
{
[TestMethod]
public void FullNameLastNameEmpty()
{
//--Arrange
Customer customer = new Customer {FirstName = "John"};
const string expected = "John";
//--Act
string actual = customer.FullName;
//--Assert
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void StaticTest()
{
//--Arrange
Customer[] customers =
{
new Customer {FirstName = …Run Code Online (Sandbox Code Playgroud) 我可能遗漏了一些东西,但我正在使用一个使用大量
typedef enum foo
{
....
} foo;
Run Code Online (Sandbox Code Playgroud)
这与枚举类相同但不是强类型的吗?
c++ ×3
c# ×1
c++17 ×1
destructor ×1
enum-class ×1
enums ×1
mstest ×1
templates ×1
typedef ×1
unit-testing ×1