下面的snipplet是否正确用于取消定义所有其他方法和类的构造函数?
struct Picture {
// 'explicit': no accidental cast from string to Picture
explicit Picture(const string &filename) { /* load image from file */ }
// no accidental construction, i.e. temporaries and the like
Picture() = delete;
// no copy
Picture(const Picture&) = delete;
// no assign
Picture& operator=(const Picture&) = delete;
// no move
Picture(Picture&&) = delete;
// no move-assign
Picture& operator=(Picture&&) = delete; // return type correct?
};
Run Code Online (Sandbox Code Playgroud)
这会删除每个默认的编译器实现,只留下析构函数,对吧?没有它,我猜这个类(几乎)无法使用,但我也可以删除它,对吗?
Picture&move-assign 的返回类型operator=(Picture&&)是否正确?如果我Picture&&为回归类型写的,它会有所作为吗?
c++ operator-overloading rvalue-reference move-semantics c++11
是std::array<int,10>(没有我自己使用new)保证在堆栈中分配,而不是由C++ - Standard分配?
要清楚,我不是故意的new std::array<int, 10>.我主要想知道,如果允许标准库new在其实现中使用.
它说的是C++ 11 FDIS
如果使用virt-specifier override标记虚函数并且不覆盖基类的成员函数,则该程序格式错误.[例如:
Run Code Online (Sandbox Code Playgroud)struct B { virtual void f(int); }; struct D : B { void f(long) override; // error: wrong signature overriding B::f void f(int) override; // OK };
如果B::f没有被标记为虚拟怎么办?那个程序是不是形成了吗?或者override然后被忽略.我在std文本中找不到任何处理此案例的内容.
更新1/2(合并)我将请求转发给C++编辑器以查看事物.谢谢约翰内斯向我指出这一点.
但是通过实现这一点,我发现,无法满足"覆盖"上下文关键字的意图:如果函数名称中的拼写错误或错误的参数类型确实使函数本身非虚拟,那么标准的文本永远不会适用 - - 并且"覆盖"变得无用.
最好的解决方案可能是
C++ 11将允许将类和虚方法标记为最终,以禁止从它们派生或覆盖它们.
class Driver {
virtual void print() const;
};
class KeyboardDriver : public Driver {
void print(int) const final;
};
class MouseDriver final : public Driver {
void print(int) const;
};
class Data final {
int values_;
};
Run Code Online (Sandbox Code Playgroud)
这非常有用,因为它告诉读者接口有关使用此类/方法的意图.如果用户尝试覆盖,则用户获得诊断也可能有用.
但编译器的观点是否有优势?当编译器知道"这个类永远不会从中派生出来"或"这个虚拟函数永远不会被覆盖"时,编译器能做些什么吗?
因为final我主要发现只有N2751指的是它.通过一些讨论,我发现了来自C++/CLI方面的论据,但没有明确暗示为什么final对编译器有用.我正在考虑这个问题,因为我也看到了标记类的一些缺点final:要对受保护的成员函数进行单元测试,可以派生一个类并插入测试代码.有时这些课程是很好的候选人final.在这些情况下,这种技术是不可能的.
如果我有一段代码
MyIdentifierIsNice(OtherThingAlsoNice isBetterThen);
我想在Eclipse中停止Ctrl-Left的行为:
My|Identifier|Is|Nice|(|Other|Thing|Also|Nice is|Better|Then|);|
到这里:
MyIdentifierIsNice(|OtherThingAlsoNice |isBetterThen);|
......或者至少不是那么频繁.其他变体也可以,例如:
MyIdentifierIsNice|(|OtherThingAlsoNice| isBetterThen|);|
主要是它应该停止考虑CamelCaseIdentifier由几个单词组成,用于通过Next-Word进行导航,等等.
我猜我使用的是SpringSourceSuite版本2.5.1,它是Eclipse 3.6.
假设我使用基于范围的循环编程时的当前规则说
使用
for(auto const &e :...)或for(auto &e:...)在可能时使用for(auto a: ...).
我以我自己的经验和这个问题为例.
但是在读完关于循环的新简洁之后我想知道,我不应该用我&的规则替换我的规则&&吗?正如这里所写,这看起来像迈耶斯的通用参考.
所以,我问自己,如果我的新规则要么
使用
for(auto const &&e :...)或for(auto &&e:...)在可能的时候......
或者这不总是有效,因此应该是相当复杂的
检查
for(auto const &&e :...)或者for(auto &&e:...)是可能的,再考虑for(auto const &e :...)或者for(auto &e:...),并且只在需要时不使用引用.
我是一个新手Java编码器,我只是读取一个整数类的变量,可以在API中描述三种不同的方式.我有以下代码:
if (count.compareTo(0)) {
System.out.println(out_table);
count++;
}
Run Code Online (Sandbox Code Playgroud)
这是一个循环,只是输出out_table.
我的目标是弄清楚如何查看整数值count > 0.
我意识到这count.compare(0)是正确的方法吗?或者是count.equals(0)吗?
我知道这count == 0是不正确的.这是正确的吗?它的价值比较运算符在哪里count=0?
将指针转换为int并稍后再返回指针是否安全?
如果我们知道指针是否为32位长且int是32位长怎么样?
long* juggle(long* p) {
static_assert(sizeof(long*) == sizeof(int));
int v = reinterpret_cast<int>(p); // or if sizeof(*)==8 choose long here
do_some_math(v); // prevent compiler from optimizing
return reinterpret_cast<long*>(v);
}
int main() {
long* stuff = new long(42);
long* ffuts = juggle(stuff);
std::cout << "Is this always 42? " << *ffuts << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这是否包含在标准中?
C++中的特征封装了一系列操作,这些操作允许算法或数据结构使用与其实例化的类型的操作符.char_traits是分组string和文件所需功能的示例.
但并非所有特质都在他们的名字中有"特质",对吧?numeric_limits浮现在脑海中.这也是"特质"吗?即使没有名称"特质"吗?
那么,是否有其他模板可以/应该被视为"特征"?除了我发现的例子:
allocator_traits 如何获得记忆pointer_traits 如何间接访问对象type_traits 元编程char_taits 符号序列iterator_traits 如何前进,后退和元素regex_traits 为...正则表达式.我想,我要问的是,是否有一个纯粹的特征定义?
我特别不确定的一些事情是:
numeric_limits 上文提到的<chrono>定制"特征",[20.11.4],即 duration_valueshash<>算子可以被认为是一种特质吗?更新:究竟是什么让特质成为特质似乎在细节上有点争议.也许可以回答另一个问题:是否有一个全面的列表,哪些类似特征的类对C++ 0x来说是新的,哪些已经在C++ 03中?也许有人知道某个地方的链接?
C++ 11定义high_resolution_clock并且它具有成员类型period和rep.但我无法弄清楚如何才能获得该时钟的精度.
或者,如果我可能达不到精度,我能以某种方式至少得到一个在纳秒之间的最小可表示持续时间的计数吗?可能用period?
#include <iostream>
#include <chrono>
void printPrec() {
std::chrono::high_resolution_clock::rep x = 1;
// this is not the correct way to initialize 'period':
//high_resolution_clock::period y = 1;
std::cout << "The smallest period is "
<< /* what to do with 'x' or 'y' here? */
<< " nanos\n";
}
Run Code Online (Sandbox Code Playgroud) c++ ×8
c++11 ×6
arrays ×1
attributes ×1
autoboxing ×1
c++-chrono ×1
clock ×1
duration ×1
eclipse ×1
editor ×1
equals ×1
final ×1
for-loop ×1
int ×1
integer ×1
java ×1
overriding ×1
pointers ×1
stack ×1
text-editor ×1
traits ×1
type-traits ×1
virtual ×1