在尝试估计性能之间的差异时push_back
,std::inserter
我遇到了一个非常奇怪的性能问题.
我们考虑以下代码:
#include <vector>
using container = std::vector<int>;
const int size = 1000000;
const int count = 1000;
#ifdef MYOWNFLAG
void foo(std::insert_iterator<container> ist)
{
for(int i=0; i<size; ++i)
*ist++ = i;
}
#endif
void bar(container& cnt)
{
for(int i=0; i<size; ++i)
cnt.push_back(i);
}
int main()
{
container cnt;
for (int i=0; i<count; ++i)
{
cnt.clear();
bar(cnt);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,无论是否MYOWNFLAG
定义,都不会调用函数foo.但是,此标志的值对性能有影响:
$ g++ -g -pipe -march=native -pedantic -std=c++11 -W -Wall -Wextra -Werror -O3 -o …
Run Code Online (Sandbox Code Playgroud) 我刚刚重新启动了一个暂停了几个月的项目.上次我编译它它工作得很好,没有任何错误或警告.然而,当我今天早些时候尝试编译它时,我得到了这个警告
attention : ‘template<class _Operation> class std::binder2nd’ is deprecated [-Wdeprecated-declarations]
Run Code Online (Sandbox Code Playgroud)
这个警告字面上出现了数百次,包括我在项目中使用的Eigen/Geometry
In file included from [...]/include/Eigen/src/Core/ArrayBase.h:109:0,
from [...]/include/Eigen/Core:350,
from [...]/include/Eigen/Geometry:4,
from [...]/include/[myproject]/types.hh:8,
from [...]/include/[myproject]/voronoi.hh:8
Run Code Online (Sandbox Code Playgroud)
从那以后我没有更新Eigen(使用3.2.4,这仍然是今天的最后一次更新).但是,自从我上次编译它以来,GCC已经更新到5.1.0(我使用的是archlinux)
题:
我推测这std::bind2nd
是非常弃用的,并且已经完成了提交以解决在Eigen中的问题.然而,这个提交尚未与主分支合并:/(并没有解决问题,因为一些std::bind2nd
仍存在于Eigen的代码中)
底线是:不推荐使用Eigen的最后一个稳定版本
我已经知道你可以启用(或不启用)类的方法 std::enable_if
举个例子:
template<size_t D, size_t E>
class Field
{
...
size_t offset(const std::array<float,D>& p) const
{
...
}
template<typename TT = size_t>
typename std::enable_if<D!=E, TT>::type
offset(const std::array<float,E>& p) const
{
return offset(_projection(p));
}
...
};
Run Code Online (Sandbox Code Playgroud)
这有助于无法调用在特定情况下无效的函数以及删除重载错误......对我而言,这非常好!
我想进一步让我的班级成员只在需要时出席.这样,如果我尝试使用一个原本不会被启动的异议,我会收到错误
我试着这样做
template<size_t D, size_t E>
class Field
{
...
template<typename TT = projectionFunc>
typename std::enable_if<D!=E, TT>::type _projection;
}
Run Code Online (Sandbox Code Playgroud)
但是编译器告诉我:
erreur: data member ‘_projection’ cannot be a member template
Run Code Online (Sandbox Code Playgroud)
有没有办法实现我想要的?
我正在开发一个Qt应用程序
我设法使用方法设置主窗口标题
setWindowTitle("my title");
Run Code Online (Sandbox Code Playgroud)
但是这会更改应用程序顶部栏中的名称,它不会影响gnome shell(ubuntu)顶部栏中显示的标题,也不会影响执行alt + tab时显示的标题.
怎么改呢?
我尝试在.pro文件中使用DEPLOYMENT.display_name变量
在我的~/.vimrc
设置选项卡给我2个空格长
set shiftwidth=2
set tabstop=2
Run Code Online (Sandbox Code Playgroud)
但是,当我打开.py
文件时,标签长度为4个空格.我没有python文件的具体配置.~/.vim/after
是空的,搜索py
不会引发任何可疑线.
你有过这样的经历吗?如何解决这样的行为?
我相信现代C++初始化列表对于初始化对象非常有用,从而无需定义自己的构造函数:
struct point
{
float coord[3];
};
point p = {1.f, 2.f, 3.f}; // nice !
Run Code Online (Sandbox Code Playgroud)
但是,当我的类继承自另一个类时,这不起作用:
template<typename T>
class serializable
{
protected:
serializable() = default;
...
// other stuff
}
struct point : public serializable<point>
{
float coord[3];
};
point p = {1.f, 2.f, 3.f}; // Doesn't work :(
Run Code Online (Sandbox Code Playgroud)
我尝试添加point() = default;
到我的点类,但这也不起作用.如何使用初始化列表初始化点?
我很惊讶没有在 boost::asio(我们任何广泛使用的库)中找到时钟组件,所以它尝试制作一个简单、简约的实现来测试我的一些代码。
使用boost::asio::deadline_timer
我做了以下课程
class Clock
{
public:
using callback_t = std::function<void(int, Clock&)>;
using duration_t = boost::posix_time::time_duration;
public:
Clock(boost::asio::io_service& io,
callback_t callback = nullptr,
duration_t duration = boost::posix_time::seconds(1),
bool enable = true)
: m_timer(io)
, m_duration(duration)
, m_callback(callback)
, m_enabled(false)
, m_count(0ul)
{
if (enable) start();
}
void start()
{
if (!m_enabled)
{
m_enabled = true;
m_timer.expires_from_now(m_duration);
m_timer.async_wait(boost::bind(&Clock::tick, this, _1)); // std::bind _1 issue ?
}
}
void stop()
{
if (m_enabled)
{
m_enabled = false;
size_t c_cnt = …
Run Code Online (Sandbox Code Playgroud) 我想对我正在设计的网页进行特定的设计。
主包装包含连续的<div class='section'>
和<div class='section-header'>
。会section-header
在图像上显示该部分的标题。
例子:
<div id="tag" class="section-header">
<h1>Title</h1>
<img src="assets/img/some_image.jpg">
</div>
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的CSS是:
.section-header
{
width: 100%;
height: 192px;
overflow: hidden;
}
.section-header > *
{
width: 100%;
line-height: 192px;
margin: 0;
}
.section-header > h1
{
position: absolute;
z-index: 10000;
text-align: center;
}
.section-header > img
{
filter: opacity(50%);
}
Run Code Online (Sandbox Code Playgroud)
但是我想在背景图片和之间添加一些相对运动section-header
。我基本上想将图像固定在屏幕上,position: fixed;
然后overflow: none;
完成工作。
但是,看来,一旦添加position: fixed; top: 0;
到.section-header > img
,溢出就不再被隐藏,并且无论嵌套标题的位置如何,图像都是可见的。
我该如何解决?
编辑:开发代码在这里可见。我基本上会在每个部分的标题后面添加图像,而不是随页面一起显示,而只是让该部分的标题在您显示时显示出来
我想要做的是让一些类从一个extention
类继承。问题是extention
类必须知道它正在扩展哪个类。
这可以简单地实现如下:
template<typename Self>
class Extention
{
public:
void check() const
{
std::cout << "Extention is valid: "
<< std::boolalpha
<< std::is_base_of<Extention, Self>::value
<< std::endl;
}
};
class Foo : public Extention<Foo> {};
class Bar : public Extention<void> {};
Run Code Online (Sandbox Code Playgroud)
在Foo
与Bar
类节目好,进一步扩展的不好的用法。
Foo().check(); ? Extention is valid: true
Bar().check(); ? Extention is valid: false
Run Code Online (Sandbox Code Playgroud)
我想在编译时检查模板的有效性,这让我写了
template<typename Self>
class Extention
{
static_assert(std::is_base_of<Extention, Self>::value);
};
Run Code Online (Sandbox Code Playgroud)
但是,gcc 告诉我这static_assert
是错误的,因为class Foo
类型不完整。
我究竟做错了什么 ?
编辑:我正在使用-std=c++17 …
这是向前方向的cumsum:
> import numpy as np
> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
> np.cumsum(np.arange(10))
array([ 0, 1, 3, 6, 10, 15, 21, 28, 36, 45])
Run Code Online (Sandbox Code Playgroud)
我想反向执行cumsum,这会给我
array([45, 45, 44, 42, 39, 35, 30, 24, 17, 9])
Run Code Online (Sandbox Code Playgroud)
最简单,最有效的方法是什么?
c++ ×6
c++11 ×3
gcc ×2
inheritance ×2
python ×2
bind2nd ×1
boost ×1
boost-asio ×1
callback ×1
css ×1
css-position ×1
eigen ×1
enable-if ×1
gnome ×1
html ×1
indentation ×1
linux ×1
member ×1
numpy ×1
overflow ×1
performance ×1
qt ×1
templates ×1
vim ×1