小编Gia*_*cci的帖子

在C++ 11中从函数返回元组的最佳方法是什么?

我想从函数中返回一些值,我想将它打包在一个元组中.所以我有两种功能声明的可能性:

std::tuple<bool, string, int> f()
{
  ...
  return std::make_tuple(false, "home", 0);
}
Run Code Online (Sandbox Code Playgroud)

std::tuple<bool, string, int> f()
{
  ...
  return std::forward_as_tuple(false, "home", 0);
}
Run Code Online (Sandbox Code Playgroud)

这些功能是等价的?在您喜欢的这些功能之间?

c++ c++11 stdtuple

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

Elixir有垃圾收集器吗?

我开始阅读有关Elixir编程语言的内容.

我明白那个:

  1. 它是功能性的
  2. 它是动态的,但支持@spec
  3. 它基于Erlang VM

我的问题是:它有一种GC吗?

elixir

13
推荐指数
1
解决办法
2559
查看次数

std :: bind类成员函数

我有这个代码:

#include <iostream>
#include <functional>

struct Foo
{
        int get(int n) { return 5+n; }
};

int main()
{
        Foo foo;
        auto L = std::bind(&Foo::get, &foo, 3);

        std::cout << L() << std::endl;

        return 0;
}
Run Code Online (Sandbox Code Playgroud)

似乎这个:

auto L = std::bind(&Foo::get, &foo, 3);
Run Code Online (Sandbox Code Playgroud)

相当于:

auto L = std::bind(&Foo::get, foo, 3);
Run Code Online (Sandbox Code Playgroud)

为什么?

c++ c++11

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

在C++ 11中从std :: deque移动元素

我们知道std::deque::front()返回对deque的第一个元素的引用.我想知道这段代码是否总是安全的:

//deque of lambdas
deque<function<void(void)>> funs;

// then is some other place:
// take a lock
m.lock();
auto f = move(funs.front()); // move the first lambda in f
funs.pop_front(); // remove the element from deque //now the value is hold by f
m_.unlock(); // unlock the resorce
f(); //execute f
Run Code Online (Sandbox Code Playgroud)

我已经尝试使用gcc-4.9这个代码并且有效,但我不知道我们是否可以认为这个代码是安全的!

c++ move std deque c++11

6
推荐指数
1
解决办法
3433
查看次数

质量算法上的Scala性能

我对Scala很新,所以为了开始编写一些代码,我已经实现了这个简单的程序:

package org.primes.sim

object Primes {

  def is_prime(a: Int): Boolean = {
    val l = Stream.range(3, a, 2) filter { e => a % e == 0}
    l.size == 0
  }

  def gen_primes(m: Int) = 
    2 #:: Stream.from(3, 2) filter { e => is_prime(e) } take m

  def primes(m : Int) = {
      gen_primes(m) foreach println      
  }

  def main(args: Array[String]) {
    if (args.size == 0)
      primes(10)
    else
      primes(args(0).toInt)
  }

}
Run Code Online (Sandbox Code Playgroud)

它从2开始生成n个素数.然后我使用Eric Nibler的range-v3库在C++ 11中实现了相同的算法.这是代码:

#include <iostream>
#include <vector>
#include …
Run Code Online (Sandbox Code Playgroud)

performance scala c++11

6
推荐指数
1
解决办法
431
查看次数

C++:使用boost :: hana将数组元素扩展为函数的参数

我两个月前发现了boost :: hana.看起来非常强大所以我决定看一看.从文档中我看到了这个例子:

std::string s;
hana::int_c<10>.times([&]{ s += "x"; });
Run Code Online (Sandbox Code Playgroud)

这相当于:

s += "x"; s += "x"; ... s += "x"; // 10 times
Run Code Online (Sandbox Code Playgroud)

我想知道是否有可能(如果是的话)写smthg:

std::string s;
std::array<int, 10> xs = {1, 3, 5, ...};
hana::int_c<10>.times([&](int i){ s += std::to_string(xs[i]) + ","; });
Run Code Online (Sandbox Code Playgroud)

在编译时,甚至是一种"解包":

myfunction( hana::unpack<...>( xs ) );
Run Code Online (Sandbox Code Playgroud)

c++ boost c++14 boost-hana

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

我如何能够获得指向通用引用的指针?

我有这个代码:

char X[64];

template <typename E>
void f (E &&e, size_t len)
{
    memset(X, 0, 64);
    memcpy(X, &e, len);
}
Run Code Online (Sandbox Code Playgroud)

用这个打电话:

const char* tx = "hello!";
f(tx, strlen(tx));
Run Code Online (Sandbox Code Playgroud)

但是当我打印变量X不是我想要的!我认为我对e的通用引用(即E && e)被认为是不正确的.

任何的想法?

c++ templates c++11

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

在Elixir中重新分配变量时出现意外行为(绑定运算符)

在Elixir中,"="运算符是"绑定"运算符.所以我想知道为什么这段代码有效:

a = 0
a = a + 1
Run Code Online (Sandbox Code Playgroud)

如果我们检查"a"的值现在是1.我本来希望看到一个绑定错误消息,如:

** (MatchError) no match of right hand side value: 0
Run Code Online (Sandbox Code Playgroud)

如何重新绑定操作?

elixir

0
推荐指数
1
解决办法
203
查看次数

C++ 11是否引入了任何新的方法来打破嵌套的for循环?

考虑的情况下进行的其他内部

int f( ... )
{
  for (int i = start_a; i < end_a; i++)
  {
    for (int j = start_b; j < end_b; j++)
    {
      // make some computation
      if( i_must_exit == true)
      {
        // exit from all for
      }
    }
  }

  // I want arrive here
}
Run Code Online (Sandbox Code Playgroud)

我们想要摆脱两个for循环.在没有分解内部函数,抛出异常等的情况下,这在C++ 03中并不容易.我想知道C++ 11是否引入了一种机制来执行此操作.

c++ idioms c++11

-3
推荐指数
1
解决办法
623
查看次数

标签 统计

c++ ×6

c++11 ×6

elixir ×2

boost ×1

boost-hana ×1

c++14 ×1

deque ×1

idioms ×1

move ×1

performance ×1

scala ×1

std ×1

stdtuple ×1

templates ×1