目标:
我想为我的调试版本提供范围检查版本的std::vectors operator [],并且在发布模式下没有范围检查.
调试模式下的范围检查显然对调试有好处,但是在我的发布代码中导致速度减慢5% - 10%,我想避免.
可能的解决方案:
我在Stroustrup的"The C++编程语言"中找到了一个解决方案.他做了以下事情:
template <class T>
class checked_vector : public std::vector<T> {
public:
using std::vector<T>::vector;
//override operator [] with at()
};
Run Code Online (Sandbox Code Playgroud)
这是有问题的,因为它继承自具有非虚拟析构函数的类,这是危险的.(并且休息室 不太 喜欢那个解决方案.)
另一个想法是这样的类:
template <class T>
class checked_vector {
std::vector<T> data_;
public:
//put all public methods of std::vector here by hand
};
Run Code Online (Sandbox Code Playgroud)
这既繁琐又会产生大量的复制粘贴,这也很糟糕.
上述解决方案的优点在于我可以在makefile中使用宏定义来简单地打开和关闭它们.
问题:
我有一个概念上的问题从C移植到Python:
int p;
for (p = 32; p < 64; p += 2) {
if (some condition)
break;
do some stuff
}
return p;
Run Code Online (Sandbox Code Playgroud)
将循环转换为for p in range(32,64,2)不起作用.这是因为在循环结束后,p等于62而不是64.
我可以while轻松地使用循环:
p = 32
while p < 64:
if (some condition):
break
do some stuff
p += 2
return p
Run Code Online (Sandbox Code Playgroud)
但我正在寻找一种Pythonic方式.
有人可以解释为什么以下代码为f2打印0?似乎= {}以某种方式解析为int赋值运算符.
#include <iostream>
struct Foo
{
Foo() : x {-1} {}
Foo(int x) : x{x} {}
Foo& operator=(int y) { x = y; return *this; }
Foo& operator=(const Foo& f) { x = f.x; return *this; }
int x;
};
int main()
{
Foo f1 = {};
Foo f2;
f2 = {};
std::cout << f1.x << '\n'; // this prints -1
std::cout << f2.x << '\n'; // this prints 0
}
Run Code Online (Sandbox Code Playgroud) 考虑到cppreference和当前的c ++工作草案,如果符合以下条件,则可以轻松复制一个类:
琐碎的非删除析构函数
所以我想出了这个代码示例:
#include <type_traits>
struct non_trivially_copyable {
non_trivially_copyable(non_trivially_copyable const&) = delete;
non_trivially_copyable& operator=(non_trivially_copyable const&) = delete;
non_trivially_copyable(non_trivially_copyable &&) = delete;
non_trivially_copyable& operator=(non_trivially_copyable &&) = delete;
};
int main()
{
return std::is_trivially_copyable<non_trivially_copyable>::value;
}
Run Code Online (Sandbox Code Playgroud)
我的班级不满足要求编号5.它仍然给我的结果是我的班级non_trivially_copyable可以轻易复制.我在一些在线编译器上测试了它:
我怀疑所有的实现都是错误的; 那为什么我得到这个结果呢?
即使我有一台强大而快速的计算机(Pentium Dual Core 2.0和2Gb RAM),我总是在寻找轻量级软件,因此即使许多应用程序同时启动和运行,它也能快速运行.
在过去的几周里,我一直在逐渐迁移到Linux,并希望安装一个免费的轻量级但有用的IDE来编写C++和PHP程序.Sintax突出显示和代码完整提示是必备的.
所以,我想收到你们的一些建议.
公共函数是否可以返回指向类中私有变量的指针.如果是这样/如果没有,会发生什么?它会崩溃还是有什么高度不安全的?可以读取或写入指向的数据吗?谢谢
当访问某个类的成员时,我可以使用例如:
this->myVar = 10
Run Code Online (Sandbox Code Playgroud)
或者我可以写:
myVar = 10
Run Code Online (Sandbox Code Playgroud)
我喜欢使用this->它,因为它显式声明变量是这个类的成员,但与仅仅使用变量名相比,它是否会导致任何开销?
作为替代方案,我可以为变量添加一个唯一的前缀,例如_TmyVar,但我已经使用this->了很长时间,所以我只是想知道.
我只是有一个简短的问题.我无法弄清楚使用std::next而不仅仅是向指针添加所需数量的进步的好处.一个简单的例子:
int main()
{
int arr [] = {1, 2, 3, 4, 5};
cout << *(arr + 2) << ", "; //example 1
cout << *std::next(arr, 2) << endl; //example 2
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出: 3, 3
从逻辑上讲,示例1应该更快,因为没有调用函数等.此外,在我运行此代码的实例中,如果我添加了一个会导致指针超出范围的数字(例如7),编译器会在示例1中抛出一个错误,但很高兴继续在示例2中给我一个内存地址.这与我最初的想法相矛盾:std::next如果指针超出界限,会发出某种警告或某些事情.
任何启蒙都会受到赞赏.
编辑:我重新格式化了帖子以便更清楚.
为什么这样做:
struct A {};
struct B {
B(A){}
};
void operator+(const B&, const B&) {}
int main()
{
A a1, a2;
a1 + a2;
}
Run Code Online (Sandbox Code Playgroud)
这不是吗?
struct B {
B(const char*){}
};
void operator+(const B&, const B&) {} //error: invalid operands of types 'const char [6]' and 'const char [6]' to binary 'operator+'|
int main()
{
"Hello" + "world";
}
Run Code Online (Sandbox Code Playgroud)
本质上,在第一个示例中a1,a2它们都B通过隐式转换转换为对象并使用operator+(const B&, const B&)添加.
从这个例子开始,我希望"Hello"并再次通过隐式构造函数"world"转换为B对象,并使用 …
在下面的代码中,编译器无法确定我想要使用哪个构造函数.为什么,我该如何解决这个问题?(实例)
#include <tuple>
#include <functional>
#include <iostream>
template<typename data_type, typename eval_type, typename Type1, typename Type2>
class A
{
public:
using a_type = std::tuple<Type1, Type2>;
using b_type = std::tuple<std::size_t,std::size_t>;
inline explicit constexpr A(const std::function<data_type(a_type)>& Initializer,
const std::function<eval_type(data_type)>& Evaluator,
const Type1& elem1, const Type2& elem2)
{
std::cout << "idx_type" << std::endl;
}
inline explicit constexpr A(const std::function<data_type(b_type)>& Initializer,
const std::function<eval_type(data_type)>& Evaluator,
const Type1& elem1, const Type2& elem2)
{
std::cout << "point_type" << std::endl;
}
};
int main()
{
int a …Run Code Online (Sandbox Code Playgroud)