我是一个简单的程序员.我的类成员变量通常由POD类型和STL容器组成.因此,我很少需要编写赋值运算符或复制构造函数,因为这些是默认实现的.
除此之外,如果我使用std::move
不可移动的对象,它使用赋值运算符,这意味着std::move
非常安全.
由于我是一个简单的程序员,我想利用移动功能,而不需要为我编写的每个类添加移动构造函数/赋值运算符,因为编译器可以简单地将它们实现为" this->member1_ = std::move(other.member1_);...
"
但它没有(至少在Visual 2010中没有),有什么特别的原因吗?
更重要的是; 有没有办法解决这个问题?
更新: 如果你低头看GManNickG的答案,他会为此提供一个很棒的宏.如果你不知道,如果你实现了移动语义,你可以删除交换成员函数.
可以说我有这些代码行;
std::vector<int> ints;
std::for_each(ints.begin(), ints.end(), [](int& val){ val = 7; });
Run Code Online (Sandbox Code Playgroud)
但是,我不想在我的lambda函数中指定参数类型,即我想写这样的东西;
std::for_each(ints.begin(), ints.end(), [](auto& val){ val = 7; });
Run Code Online (Sandbox Code Playgroud)
无论如何这可以实现吗?
(boost :: lambda不需要指定类型......)
更新:
现在我使用宏:#define _A(container) decltype(*std::begin(container))
所以我可以这样做:
std::for_each(ints.begin(), ints.end(), [](_A(ints)& val){ val = 7; });
Run Code Online (Sandbox Code Playgroud) // This snippet
for (const float t : std::array{ 0.0f, 0.33f, 0.66f, 1.0f }) {
std::cout << "t = " << t << "\n";
}
// Yields the following (incorrect) values:
t = -3.91649e-28
t = 4.59037e-41
t = 2.66247e-44
t = 0
// Whereas this snippet ...
auto vals = std::array{ 0.0f, 0.33f, 0.66f, 1.0f };
for (const float t : vals) {
std::cout << "t = " << t << "\n";
}
// Yields the following (correct) values: …
Run Code Online (Sandbox Code Playgroud) 假设我有一个规范化函数定义为:
Vec3f Vec3f::getNormalized() const {
return (*this)/this->length();
}
Run Code Online (Sandbox Code Playgroud)
如果在没有存储它的返回值的情况下使用此函数,是否可能以某种方式创建编译时错误?;
v.getNormalized(); // which most definitely is a typo
Run Code Online (Sandbox Code Playgroud)
..代替..
v = v.getNormalized();
Run Code Online (Sandbox Code Playgroud) 这只是过去几天困扰我的事情,我认为不可能解决,但我之前看过模板魔术.
开始:
为了获得标准C++数组中的元素数量,我可以使用宏(1)或类型安全的内联函数(2):
(1)
#define sizeof_array(ARRAY) (sizeof(ARRAY)/sizeof(ARRAY[0]))
Run Code Online (Sandbox Code Playgroud)
(2)
template <typename T>
size_t sizeof_array(const T& ARRAY){
return (sizeof(ARRAY)/sizeof(ARRAY[0]));
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,第一个问题是成为一个宏(目前我认为是一个问题),而另一个问题是在编译时无法获得数组的大小; 即我不能写:
enum ENUM{N=sizeof_array(ARRAY)};
Run Code Online (Sandbox Code Playgroud)
要么
BOOST_STATIC_ASSERT(sizeof_array(ARRAY)==10);// Assuming the size 10..
Run Code Online (Sandbox Code Playgroud)
有谁知道这是否可以解决?
更新:
这个问题是在引入constexpr之前创建的.现在你可以简单地使用:
template <typename T>
constexpr auto sizeof_array(const T& iarray) {
return (sizeof(iarray) / sizeof(iarray[0]));
}
Run Code Online (Sandbox Code Playgroud) 根据http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html,EASTL的
vector<uint64>::operator[]
速度比"常用的STL商业版本"快2%到70%.
除非STL的商业版本使用范围检查,这会使比较不公平,对于这样一个简单的操作,它怎么可能是这样的速度差异?
更新:
似乎答案是,EA工程师只是通过与使用范围检查的版本进行比较而作弊...
鉴于此函数和函数调用:
std::string GetString() {
std::stringstream sstr;
const auto str = sstr.str();
return str;
}
const auto returnedStr = GetString();
Run Code Online (Sandbox Code Playgroud)
当我声明str
为const时,是否会省略move-construction ?
当编译器符合Cpp0x时,是否有官方或非官方的#defines?更好的是,对于特定的Cpp0x功能(〜#cpp0xlambda,#cpp0xrvalue等)?
(网上没有发现任何相关信息)
据我所知,C++ 17有两种统一调用语法的道具(其中另一种称为统一调用语法).
阅读它们,我无法看到它们打算如何处理名称空间.
例:
class Class {...}
namespace MyNamespace {
void f(Class x, Class y);
}
Run Code Online (Sandbox Code Playgroud)
是否可以使用以下方法调用此方法:
Class a, b;
a.MyNamespace::f(b);
Run Code Online (Sandbox Code Playgroud)
或者同时执行free函数,并且需要在同一名称空间中定义类?
参考文献:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4174.pdf
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4165.pdf
我刚碰到这行代码:
if( lineDirection.length2() ){...}
Run Code Online (Sandbox Code Playgroud)
哪里length2
返回一个double
.让我感到困惑的是,0.0等于0 NULL
,和/或false
.
这是C++标准的一部分还是未定义的行为?
c++ ×10
c++11 ×3
c++17 ×1
eastl ×1
lambda ×1
namespaces ×1
optimization ×1
pod ×1
puzzle ×1
return-value ×1
stdarray ×1
stl ×1