小编use*_*108的帖子

为什么`std :: initializer_list`经常按值传递?

几乎我在SO上看到的所有帖子都涉及到std::initializer_list,人们倾向于传递一个std::initializer_list值.根据这篇文章:

http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/

如果想要传递传递的对象的副本,则应该按值传递.但复制a std::initializer_list并不是一个好主意

复制a std::initializer_list不会复制基础对象.在原始初始化程序列表对象的生存期结束后,不保证基础数组存在.

那么为什么它的一个实例经常被价值所传递,而不是通过const&哪个保证不会产生不必要的副本呢?

c++ c++11

62
推荐指数
2
解决办法
8764
查看次数

std :: string :: c_str()和temporaries

以下C++代码是否格式良好:

void consumer(char const* p)
{
  std::printf("%s", p);
}

std::string random_string_generator()
{
  // returns a random std::string object
}

consumer(random_string_generator().c_str());
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,在创建临时std :: string对象并获取c_str()指针后,没有什么能阻止std :: string对象被破坏(或者我错了?).如果代码一切正常,你能指点我的标准吗?当我使用g ++进行测试时,它确实有效.

c++ stl stdstring

56
推荐指数
3
解决办法
7924
查看次数

为什么编译器不将浮点*2优化为指数增量?

我经常注意到gcc将乘法转换为可执行文件中的移位.当乘以a int和a 时,可能会发生类似的事情float.例如,2 * f可能只是将指数递增f1,从而节省了一些周期.编译器,也许是一个人请求他们这样做(例如通过-ffast-math),一般来说,这样做吗?

编译器通常是否足够聪明,或者我是否需要使用scalb*()ldexp()/frexp()函数系列自己完成此操作?

c c++ optimization performance compiler-optimization

46
推荐指数
4
解决办法
3844
查看次数

为什么strcmp没有SIMD优化?

我试图在x64计算机上编译这个程序:

#include <cstring>

int main(int argc, char* argv[])
{
  return ::std::strcmp(argv[0],
    "really really really really really really really really really"
    "really really really really really really really really really"
    "really really really really really really really really really"
    "really really really really really really really really really"
    "really really really really really really really really really"
    "really really really really really really really really really"
    "really really really really really really really really really"
    "really really really really really really …
Run Code Online (Sandbox Code Playgroud)

c++ sse simd strcmp sse2

36
推荐指数
3
解决办法
7563
查看次数

将负浮点值转换为unsigned int的行为是什么?

如果将负浮点值转换为无符号整数类型的值,会发生什么?标准报价将不胜感激.我面临的问题是从变量类转换为无符号整数类型的值,它包含一个浮点类型的对象.

例:

unsigned i = -.1;
Run Code Online (Sandbox Code Playgroud)

c floating-point language-lawyer

35
推荐指数
3
解决办法
2260
查看次数

传递任何函数作为模板参数

我想将函数作为模板参数传递给函数.目前我设法做的最好的是:

template< typename F, F f >
void pass()
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

...使用的:

pass< decltype(&func), &func >();
Run Code Online (Sandbox Code Playgroud)

我真正想要的是:

pass< &func >();
Run Code Online (Sandbox Code Playgroud)

有没有办法在没有宏的情况下实现这个目标?基本上同时传递类型和值?编译器显然拥有所需的所有信息......

解决方案必须使用变量参数和返回类型.函数值在编译时使用,因此不能作为参数传递.

欢迎使用C++ 11解决方案.


编辑:用例 - 我在编译时生成绑定,我需要为每个传递的函数创建一个C++函数.这段代码的用例看起来(简化)或多或少像这样:

template < typename F, F f > 
int function_wrapper( lua_State* L ) 
{
    return dispatcher<typename return_type<F>::type>::call( L, 1, f );
}

void register_native_function( lua_Function f, const char* name )
{
    // binding call using pure C function f
}

template < typename F, F f >
void register_function( const …
Run Code Online (Sandbox Code Playgroud)

c++ templates function-pointers c++11

34
推荐指数
2
解决办法
4262
查看次数

强制在基于范围的内容中使用cbegin()/ cend()

这个问题涉及:

我什么时候应该使用新的ranged-for,我可以将它与新的cbegin/cend结合使用吗?

基于这个问题,强制使用cbegin()cend()需要做的,例如:

for (auto& v: const_cast<decltype(container) const>(container))
Run Code Online (Sandbox Code Playgroud)

这是一个应该消除它的构造的很多样板代码.有更紧凑的方法吗?我的问题的原因是,隐式共享容器可能会将我的使用begin()作为分离自己的线索.

c++

31
推荐指数
3
解决办法
4338
查看次数

获取进程的argc和argv的替代方法

我正在寻找替代方法来获取命令行参数argcargv提供给进程,而无需直接访问传入的变量main().

我想使一类是独立的main(),这样argcargv没有明确地传递给使用它们的代码.

编辑:有些澄清似乎是有序的.我有这门课.

class Application
{
  int const argc_;
  char const** const argv_;

public:
  explicit Application(int, char const*[]);
};

Application::Application(int const argc, char const* argv[]) :
  argc_(argc),
  argv_(argv)
{
}
Run Code Online (Sandbox Code Playgroud)

但我想一个默认的构造Application::Application(),有一些(最有可能)的C代码,拉动argcargv从某处.

c++ linux windows bsd posix

21
推荐指数
6
解决办法
8203
查看次数

如何编写constexpr swap函数来改变整数的endianess?

如何编写一个constexpr函数来交换整数的endianess,而不依赖于编译器扩展,你能举例说明如何做到这一点吗?

c++ endianness constexpr c++14 c++17

20
推荐指数
1
解决办法
2435
查看次数

快速平方根优化?

如果您查看这个非常好的页面:

http://www.codeproject.com/Articles/69941/Best-Square-Root-Method-Algorithm-Function-Precisi

你会看到这个程序:

#define SQRT_MAGIC_F 0x5f3759df 
 float  sqrt2(const float x)
{
  const float xhalf = 0.5f*x;

  union // get bits for floating value
  {
    float x;
    int i;
  } u;
  u.x = x;
  u.i = SQRT_MAGIC_F - (u.i >> 1);  // gives initial guess y0
  return x*u.x*(1.5f - xhalf*u.x*u.x);// Newton step, repeating increases accuracy 
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:有没有特别的理由为什么这不实现为:

#define SQRT_MAGIC_F 0x5f3759df 
 float  sqrt2(const float x)
{

  union // get bits for floating value
  {
    float x;
    int i;
  } u;
  u.x = …
Run Code Online (Sandbox Code Playgroud)

c optimization

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