我有这个代码不起作用,但我认为意图很明确:
testmakeshared.cpp
#include <memory>
class A {
public:
static ::std::shared_ptr<A> create() {
return ::std::make_shared<A>();
}
protected:
A() {}
A(const A &) = delete;
const A &operator =(const A &) = delete;
};
::std::shared_ptr<A> foo()
{
return A::create();
}
Run Code Online (Sandbox Code Playgroud)
但是我在编译时遇到了这个错误:
g++ -std=c++0x -march=native -mtune=native -O3 -Wall testmakeshared.cpp
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr.h:52:0,
from /usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/memory:86,
from testmakeshared.cpp:1:
testmakeshared.cpp: In constructor ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc) [with _Tp = A, _Alloc = std::allocator<A>, __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’:
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr_base.h:518:8: instantiated from ‘std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& …Run Code Online (Sandbox Code Playgroud) 我发现自己刚刚写了这篇文章:
template <long int T_begin, long int T_end>
class range_class {
public:
class iterator {
friend class range_class;
public:
long int operator *() const { return i_; }
const iterator &operator ++() { ++i_; return *this; }
iterator operator ++(int) { iterator copy(*this); ++i_; return copy; }
bool operator ==(const iterator &other) const { return i_ == other.i_; }
bool operator !=(const iterator &other) const { return i_ != other.i_; }
protected:
iterator(long int start) : i_ (start) { } …Run Code Online (Sandbox Code Playgroud) 默认情况下,从getter函数返回副本(1)或引用(2)会更好吗?
class foo {
public:
std::string str () { // (1)
return str_;
}
const std::string& str () { // (2)
return str_;
}
private:
std::string str_;
};
Run Code Online (Sandbox Code Playgroud)
我知道2)可能更快,但不必因(N)RVO.1)关于悬挂引用更安全,但对象可能会过时或永远不会存储引用.
当你写一个课程时,你的默认值是什么,而且还不知道(但)性能和生命周期问题是否重要?
附加问题:当成员不是普通字符串而是向量时,游戏会改变吗?
我正在写一个程序,我希望有这样的参数:
--[no-]foo Do (or do not) foo. Default is do.
Run Code Online (Sandbox Code Playgroud)
有没有办法让argparse为我这样做?
我正在使用Python 3.2
在我看来,当有人放入一个恰好与根级命名空间同名的新命名空间并且神秘地改变了许多程序的含义时,使用未锚定的命名空间只会引发麻烦.那么,为什么人们总是说std::而不是::std::.他们真的应该说"我想用任何std方便的东西,而不是根本的东西."?
这是我的意思的一个例子:
在fred/Foo.h中:
#include <string>
namespace fred {
class Foo {
public:
void aPublicMember(const std::string &s);
};
} // end namespace fred
Run Code Online (Sandbox Code Playgroud)
在fred/Bar.h中:
namespace fred {
namespace std { // A standard fred component
class string { // Something rather unlike the ::std::string
// ...
};
} // namespace std
class Bar {
public:
void aPublicMember(std::string &s);
};
} // namespace fred
Run Code Online (Sandbox Code Playgroud)
在oops.cpp中:
#include <string>
#include "fred/Bar.h"
#include "fred/Foo.h" // Oops, the meaning of …Run Code Online (Sandbox Code Playgroud) PEP 0492增加了新的__await__魔法.实现此方法的对象变为类似未来的对象,可以等待使用await.很明显:
import asyncio
class Waiting:
def __await__(self):
yield from asyncio.sleep(2)
print('ok')
async def main():
await Waiting()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Run Code Online (Sandbox Code Playgroud)
好的,但如果我想调用一些已async def定义的函数而不是asyncio.sleep?我不能使用await因为__await__不是async函数,我无法使用yield from因为本机协同程序需要await表达式:
async def new_sleep():
await asyncio.sleep(2)
class Waiting:
def __await__(self):
yield from new_sleep() # this is TypeError
await new_sleep() # this is SyntaxError
print('ok')
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
我注意到<exception>在C++ 11 中有一些更有趣的声明.任何人都可以阐明它们的含义以及如何使用它们吗?
我想知道的是:
::std::nested_exception::std::throw_with_nested::std::rethrow_if_nested此外,虽然它们看起来不言自明,但了解它们如何工作可能会很好:
::std::exception_ptr::std::make_exception_ptr::std::current_exception::std::rethrow_exception刚才我偶然发现了一个事实,即C++函数floor返回您传递给它的同类型,无论是float,double还是这样.
根据该引用,该函数返回向下舍入的整数值.为什么这不是整数?
我可以访问公共图书馆中的计算机,我想尝试一些C++和其他代码.问题是没有安装g ++,我无法使用包安装它,因为我没有root访问权限.是否有"智能"方法为主文件夹中的编程创建完整环境?
我安装了gcc(我可以编译C代码).另外,我有一个一致的主文件夹.我不知道在哪里可以找到预编译的g ++,我发现只有源代码但我不知道如何处理它.我试图让他们安装这个,但它不起作用:)
我有一组范围可能看起来像这样:
[(0, 100), (150, 220), (500, 1000)]
Run Code Online (Sandbox Code Playgroud)
然后我会添加一个范围,比如说(250, 400),列表看起来像这样:
[(0, 100), (150, 220), (250, 400), (500, 1000)]
Run Code Online (Sandbox Code Playgroud)
然后我会尝试添加范围(399, 450),它会因为重叠而出错(250, 400).
当我添加新范围时,我需要搜索以确保新范围不与现有范围重叠.并且列表中的任何范围都不会与列表中的另一个范围重叠.
为此,我想要一个以排序顺序廉价维护其元素的数据结构,并且很快允许我在给定元素之前或之后找到该元素.
有没有更好的方法来解决这个问题?是否有像Python中可用的数据结构?我知道该bisect模块存在,这可能是我将使用的.但我希望有更好的东西.
编辑:我使用bisect模块解决了这个问题.这是代码的链接.这有点长,所以我不会在这里发布:
c++ ×7
python ×3
c++11 ×2
python-3.x ×2
argparse ×1
async-await ×1
c ×1
const ×1
g++ ×1
gcc ×1
linux ×1
namespaces ×1
return-value ×1
shared-ptr ×1
std ×1