class A {};
typedef shared_ptr<const A*> AConstPtr;
typedef shared_ptr<A*> APtr;
vector<APtr> ptr;
const vector<AConstPtr>* foo()
{
return &ptr;
}
Run Code Online (Sandbox Code Playgroud)
这段代码不能编译,因为"没有从向量<Aptr >*到const向量<AConstPtr >*的隐式转换"无论如何都可以使这个工作,而不创建一个新的向量,并且不使用不安全的转换?
我需要这个的原因是因为我有一个类在内部将列表存储为向量<APtr >,但需要通过其接口公开它的完全const版本.
我想知道你可以在阵列中存储多少个数字?
srand (time(NULL));
int array[10000000];
for(int i = 0; i < 10000000; i++){
array[i] = (rand() % 10000000) + 1;
}
Run Code Online (Sandbox Code Playgroud)
每次我想在数组中存储10.000.000个数字时我的程序崩溃了(Eclipse).我甚至试过Visual Studio并且它崩溃了.
所以我想知道我可以在数组中存储多少个数字或者我的代码有问题?
我有一个(第三方)类是不可复制的.我想初始化它们的数组.这是我最好的尝试:
#include <array>
class Thing
{
public:
explicit Thing(int) {}
Thing(const Thing&) = delete;
};
int main()
{
std::array<Thing, 1> things{{{100}}}; // error here
};
Run Code Online (Sandbox Code Playgroud)
GCC 4.7.2说:
错误:从初始化列表转换为'std :: array :: value_type {aka Thing}'将使用显式构造函数'Thing :: Thing(int)'
好的,但这正是我想要的 - 使用显式构造函数.我该怎么表达呢?如果我自己实际调用构造函数,那么我会收到有关正在删除的复制构造函数的错误.我不能使用std::move()因为Thing不可移动(我无法修改它).
到目前为止,我发现的唯一替代方法是/sf/answers/1117397011/,但这是不可取的,因为它是一堆额外的代码加上我需要在我使用它的地方投射"存储"(或保持一个单独的指向它,它添加了我不想要的间接).
我想要一个解决方案,在实际使用的东西时提供最大的性能,而不需要很多丑陋的样板.
这是我正在看的功能:
template <uint8_t Size>
inline uint64_t parseUnsigned( const char (&buf)[Size] )
{
uint64_t val = 0;
for (uint8_t i = 0; i < Size; ++i)
if (buf[i] != ' ')
val = (val * 10) + (buf[i] - '0');
return val;
}
Run Code Online (Sandbox Code Playgroud)
我有一个测试工具,它传递所有可能的数字,大小= 5,左边填充空格.我正在使用GCC 4.7.2.当我使用-O3编译后在callgrind下运行程序时,我得到:
I refs: 7,154,919
Run Code Online (Sandbox Code Playgroud)
当我用-O2编译时,我得到:
I refs: 9,001,570
Run Code Online (Sandbox Code Playgroud)
好的,所以-O3提高了性能(我确认一些改进来自上述功能,而不仅仅是测试工具).但是我不想完全从-O2切换到-O3,我想找出要添加的特定选项.所以我咨询man g++了一下它所说的由-O3添加的选项列表:
-fgcse-after-reload [enabled]
-finline-functions [enabled]
-fipa-cp-clone [enabled]
-fpredictive-commoning [enabled]
-ftree-loop-distribute-patterns [enabled]
-ftree-vectorize [enabled]
-funswitch-loops [enabled]
Run Code Online (Sandbox Code Playgroud)
所以我再次使用-O2编译,然后是所有上述选项.但这让我的性能比普通的-O2更差:
I refs: 9,546,017
Run Code Online (Sandbox Code Playgroud)
我发现将-ftree-vectorize添加到-O2会导致性能下降.但我无法弄清楚如何将-O3性能与任何选项组合相匹配.我怎样才能做到这一点?
如果您想自己尝试一下,这里是测试工具(将上面的parseUnsigned()定义放在#includes下):
#include <cmath> …Run Code Online (Sandbox Code Playgroud) 我有一个用户定义的文字运算符,它只对特定长度的字符串有意义,如下所示:
constexpr uint16_t operator "" _int(const char* s, std::size_t len)
{
return len == 2 ? s[0] | (s[1] << 8) : throw;
}
Run Code Online (Sandbox Code Playgroud)
这有效:
"AB"_int // equals 16961
Run Code Online (Sandbox Code Playgroud)
但这也可以编译,我不希望它:
"ABC"_int // throws at runtime
Run Code Online (Sandbox Code Playgroud)
我试过了static_assert(len == 2),但在 constexpr 函数中是不允许的。
如何"ABC"_int在编译时导致错误?
c++ static-assert compile-time-constant user-defined-literals c++11
考虑以下代码:
#include <cstdint>
static int x = 0;
const uintptr_t arithmetic()
{
static constexpr uintptr_t result = ((uintptr_t)&x) + 1u;
return result;
}
const uintptr_t bitwise()
{
static constexpr uintptr_t result = ((uintptr_t)&x) | 1u;
return result;
}
Run Code Online (Sandbox Code Playgroud)
GCC(所有版本4-9)都arithmetic()可以正常编译,但可以拒绝bitwise():
<source>: In function 'const uintptr_t bitwise()':
<source>:13:57: error: '(((uintptr_t)(& x)) | 1)' is not a constant expression
13 | static constexpr uintptr_t result = ((uintptr_t)&x) | 1u;
| ~~~~~~~~~~~~~~~~^~~~
Run Code Online (Sandbox Code Playgroud)
为什么?请注意,在其他constexpr用例中,按位-或工作正常,但不是这种情况。
请参阅https://en.cppreference.com/w/cpp/io/basic_ios/operator_bool和https://en.cppreference.com/w/cpp/io/basic_ios/operator!。
我认为这operator!是多余的,因为用户定义的operator bool.
Linux有一个名为namespaces的功能,它允许您为不同的进程提供不同的文件系统"视图".在Windows术语中,这将是有用的,例如,如果您有一个始终加载其配置的遗留程序"floyd" C:\floyd\floyd.ini.如果Windows有名称空间,你可以编写一个包装脚本来创建一个运行的命名空间,floyd这样当Alice运行脚本时,floyd会在一个C:\floyd存在但实际指向的环境中启动C:\Users\Alice\Floyd.
现在你可能会想,"好吧,只需使用软链接或硬链接并C:\floyd为别名创建别名C:\Users\Alice." 但是对于命名空间,Bob也可以运行启动脚本,但他的floyd实例(在同一台计算机上,同时运行)将会看到C:\floyd,比如说C:\Users\Bob\Program Settings\Floyd Config(或者我们喜欢的任何其他路径)的内容.
您可以在具有命名空间的Linux上执行此操作.Windows上有类似或类似的东西吗?如果它需要编写C程序就可以了,如果只能在最新版本的Windows上运行,那就没关系.
我正在尝试将对象存储在std :: set中.这些对象是来自python环境的boost :: shared_ptr <>.向集合中添加值不会导致任何麻烦.但是当我尝试擦除一个值时,即使我传递了相同的引用,它也无法工作.这是一个例子:
#include <set>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/python.hpp>
using namespace std;
using namespace boost;
using namespace boost::python;
struct Bar
{
Bar() {}
};
struct Foo
{
set< shared_ptr<Bar> > v_set;
shared_ptr<Bar> v_ptr;
Foo() {}
void add( shared_ptr<Bar> v_param ) {
cout << "storing " << v_param << "in v_set and v_ptr" << endl;
v_set.insert(v_param);
v_ptr = v_param;
}
void del( shared_ptr<Bar> v_param ) {
cout << "deleting " << v_param << endl;
if (v_param …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用MPI运行c ++代码(BCparallel.cpp); 用以下代码编译代码:
mpic++ BCparallel.cpp -o BCparallel
Run Code Online (Sandbox Code Playgroud)
很成功,但当我通过这条线
mpiexec -np 4 BCparallel file.txt
Run Code Online (Sandbox Code Playgroud)
它回来了
[proxy:0:0@lps-Inspiron-5537] HYDU_create_process
(utils/launch/launch.c:75): execvp error on file BCparallel (No such
file or directory)
[proxy:0:0@lps-Inspiron-5537] HYDU_create_process
(utils/launch/launch.c:75): execvp error on file BCparallel (No such
file or directory)
[proxy:0:0@lps-Inspiron-5537] HYDU_create_process
(utils/launch/launch.c:75): execvp error on file BCparallel (No such
file or directory)
[proxy:0:0@lps-Inspiron-5537] HYDU_create_process
(utils/launch/launch.c:75): execvp error on file BCparallel (No such
file or directory)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
c++ ×8
c++11 ×4
arrays ×2
g++ ×2
gcc ×2
shared-ptr ×2
boost ×1
boost-python ×1
casting ×1
const ×1
constexpr ×1
filesystems ×1
linux ×1
mpi ×1
namespaces ×1
noncopyable ×1
optimization ×1
portability ×1
random ×1
vector ×1
windows ×1