标签: c++

std :: initializer_list返回值的生命周期

GCC的实现破坏了std::initializer_list返回full-expression结束时从函数返回的数组.它是否正确?

此程序中的两个测试用例都显示在使用值之前执行的析构函数:

#include <initializer_list>
#include <iostream>

struct noisydt {
    ~noisydt() { std::cout << "destroyed\n"; }
};

void receive( std::initializer_list< noisydt > il ) {
    std::cout << "received\n";
}

std::initializer_list< noisydt > send() {
    return { {}, {}, {} };
}

int main() {
    receive( send() );
    std::initializer_list< noisydt > && il = send();
    receive( il );
}
Run Code Online (Sandbox Code Playgroud)

我认为该计划应该有效.但潜在的标准有点令人费解.

return语句初始化一个返回值对象,就像它被声明一样

std::initializer_list< noisydt > ret = { {},{},{} };
Run Code Online (Sandbox Code Playgroud)

initializer_list将从给定的初始化器系列初始化一个临时及其底层数组存储,然后initializer_list从第一个初始化器初始化另一个.阵列的寿命是多少?"数组的生命周期与initializer_list对象的生命周期相同." 但其中有两个; 哪一个是模棱两可的.8.5.4/6中的示例(如果它按照公布的方式工作)应该解决数组具有复制到对象的生命周期的歧义.然后返回值的数组也应该存在于调用函数中,并且应该可以通过将它绑定到命名引用来保留它.

LWS上 …

c++ lifetime initializer-list c++11 list-initialization

25
推荐指数
2
解决办法
4610
查看次数

如何设计算法来计算倒计时风格的数学数字拼图

我一直想这样做但每次我开始思考这个问题时都会因为其指数性而引起我的注意.

我希望能够理解和编码的问题解决方案是倒计时数学问题:

给定数字X1到X5的集合计算如何使用数学运算来组合Y.您可以应用乘法,除法,加法和减法.

那怎么1,3,7,6,8,3348

答案:(((8 * 7) + 3) -1) *6 = 348.

如何编写可以解决这个问题的算法?在尝试解决这样的问题时你从哪里开始?在设计这样的算法时,您需要考虑哪些重要的考虑因素?

c++ java algorithm combinatorics

25
推荐指数
4
解决办法
1万
查看次数

C++ set:计算小于值的元素

假设我有一个STL set <int> s和一个int x,我怎么能计算出s少于的元素数x

我正在寻找一个O(log n)(或类似的;任何比它更合理的O(n))解决方案;

我已经知道了std::distance(s.begin(), s.lower_bound(x)),但O(n)我相信,因为sets不是随机访问.

c++ algorithm performance complexity-theory stl

25
推荐指数
3
解决办法
5995
查看次数

德克 - 为什么"储备"不存在?

标准STL向量容器具有"保留"功能,用于保留未初始化的内存,以后可用于防止重新分配.

为什么另一个deque容器没有呢?

c++ stl

25
推荐指数
3
解决办法
1万
查看次数

Visual Studio和C++:使用过滤器作为目录

我使用Visual Studio 2012 ProfessionalC++.在创建所谓的"过滤器"并向其添加源文件时,解决方案资源管理器中的文件分为子目录,而在文件系统上,它们都在同一目录(项目目录)中

问题:我想将文件添加到过滤器,同时将其添加到文件系统上的相应目录中.除了手动移动文件外,如何实现这一目标?

c++ solution visual-studio visual-studio-2012

25
推荐指数
2
解决办法
3万
查看次数

移动语义和参数评估顺序

考虑以下因素:

std::string make_what_string( const std::string &id );

struct basic_foo
{
    basic_foo( std::string message, std::string id );
};

struct foo
    : public basic_foo
{
    foo::foo( std::string id)
        : basic_foo( make_what_string( id ), std::move( id ) ) // Is this valid?
    {
    }
};
Run Code Online (Sandbox Code Playgroud)

因为C++中的参数评估顺序是未指定的,所以我想知道是否该行

basic_foo( make_what_string( id ), std::move( id ) )
Run Code Online (Sandbox Code Playgroud)

在上面的代码是有效的.

我知道这std::move只不过是一个演员,但什么时候执行std :: string move ctor?在评估完所有参数之后,是否应该调用基础构造函数?或者这是在评估参数期间完成的?换一种说法:

编译器是否这样做:

std::string &&tmp2 = std::move(id);
std::string tmp1 = make_what_string(id);
basic_foo(tmp1, tmp2);
Run Code Online (Sandbox Code Playgroud)

这是有效的.或这个:

std::string tmp2 = std::move(id);
std::string tmp1 = make_what_string(id);
basic_foo(tmp1, …
Run Code Online (Sandbox Code Playgroud)

c++ operator-precedence move-semantics c++11

25
推荐指数
2
解决办法
522
查看次数

(不)在异常中使用std :: string

我一直在读,我不应该抛出std::string一些或其他类来分配内存.像这里或更重要的是这里的第3点- 不要嵌入std::string对象.

所以现在我正在尝试将boost :: exception插入到我的项目中,我看到了什么:很多字符串.

为什么不提升符合自己的建议?

如果我有不能硬编码的参数,比如在配置文件中安装,我怎样才能将它们放入异常中而不使用std::string

或者指南是否std::string使用std::string尽可能少的指南?我有点困惑......

我做了一些研究.如果我错了,请纠正我.


如果我理解正确,那就是关于抛出期间的分配以及分配的内存发生了什么.因此,如果我在构造函数中分配内存并且无法在异常的析构函数中释放内存,则内存会丢失,这将产生内存泄漏.但是在投掷之前分配它是可以的,所以异常是干净的.

我试过这个:

struct xexception {
  int *ttt[10];
  xexception() {
    ttt[0] = new int[0xfffffffL];
    ttt[1] = new int[0xfffffffL];
    ttt[2] = new int[0xfffffffL];
    ttt[3] = new int[0xfffffffL];
    ttt[4] = new int[0xfffffffL];
    ttt[5] = new int[0xfffffffL];
    ttt[6] = new int[0xfffffffL];
    ttt[7] = new int[0xfffffffL];
    ttt[8] = new int[0xfffffffL];
    ttt[9] = …
Run Code Online (Sandbox Code Playgroud)

c++ boost exception

25
推荐指数
2
解决办法
5282
查看次数

可以标记为Extern"C"的C++函数抛出吗?

我有C++函数我想要声明使用,extern "C"即使它们只在C++代码中调用.是的,我知道这很奇怪但是我想要保持一致性,因为我们混合了C和C++声明.我只是想确保声明一个C++函数extern "C"不会影响抛出的行为.

它看起来像这样:

extern "C" void foo() {throw exception;}

int bar()
{
    try
    {
        foo();
    } catch (exception e) { return 1; }
}
Run Code Online (Sandbox Code Playgroud)

c++ extern-c

25
推荐指数
2
解决办法
4683
查看次数

Boost Log 2.0:日志中的空严重级别

我正在使用Boost-Log 2.0,它与版本1存在一些差异,我很难输出"Severity"属性.

我正在使用"Boost.Format-style"格式化程序

"%TimeStamp% [%Uptime%] (%LineID%) <%Severity%>: %Message%"
Run Code Online (Sandbox Code Playgroud)

TimeStamp,LineIDMessagecommon_attributes.Uptime是我添加的属性attrs::timer().我认为Severity在使用时会自动添加severity_logger,但显然不是,这是我的问题.我得到空的严重性,例如:

2013-Apr-06 19:21:52.408974 [00:00:00.001337] (3) <>: A warning severity message
Run Code Online (Sandbox Code Playgroud)

注意空<>.我试图添加严重性,register_simple_formatter_factory但后来我得到编译器错误:

error: no matching function for call to ‘register_simple_formatter_factory(const char [9])’
Run Code Online (Sandbox Code Playgroud)

而且我不明白为什么.

这是我的代码:

#include <iostream>


#include <boost/log/common.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/formatter_parser.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/severity_feature.hpp>
#include <boost/log/sources/record_ostream.hpp>

#include <boost/log/attributes.hpp> …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-log

25
推荐指数
2
解决办法
1万
查看次数

C++ 11:为什么这个范围循环会将FPS降低35?

我正在使用SFML和C++ 11功能编写游戏,例如范围循环.在处理平铺地图时,我基本上为每个地图平铺创建了一个类,一个轻量级类,只包含其精灵,位置等,然后构建一些嵌套向量来表示游戏地图图层.

为了优化一次在屏幕上绘制数千个对象的过程,我只是简单地绘制了玩家看到的内容.这很顺利.

我有以下渲染游戏地图的方法,如果磁贴位置在摄像机边界内,则条件基本上返回true

void gameMap::render(sf::RenderWindow &winMain, sf::Vector2f offset) {
      for(vector<int> vec1 : backgroundData)
           for(int i : vec1)
              if(collides(i.pos, offset)
                 myWindow.draw(i.sprite);

     }
Run Code Online (Sandbox Code Playgroud)

它工作正常,但在游戏中我大致得到30 FPS,并且有很多粗糙的动作.但令我惊讶的是,下面的代码做同样的事情,呈现相同数量的瓷砖精灵,但运行速度为65 fps,运动非常流畅

void gameMap::render(sf::RenderWindow &winMain, sf::Vector2f offset) {
          for(int i = 0; i < backgroundTiles.size(); i++)
               for(int j = 0; j < backgroundTiles[i].size(); j++)
                  if(collides(backgroundTiles[i][j].pos, offset)
                     myWindow.draw(backgroundTiles[i][j].sprite);

         }
Run Code Online (Sandbox Code Playgroud)

为什么会这样?C++ 11基于范围的循环比旧学校慢得多吗?我真的很想听到这个答案,因为我的眼睛老实说更喜欢基于范围的循环,我不想发现基于范围的循环慢两倍.

c++ performance c++11

25
推荐指数
1
解决办法
2942
查看次数