我想从函数中返回一些值,我想将它打包在一个元组中.所以我有两种功能声明的可能性:
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)
这些功能是等价的?在您喜欢的这些功能之间?
我开始阅读有关Elixir编程语言的内容.
我明白那个:
我的问题是:它有一种GC吗?
我有这个代码:
#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)
为什么?
我们知道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这个代码并且有效,但我不知道我们是否可以认为这个代码是安全的!
我对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) 我两个月前发现了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) 我有这个代码:
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)被认为是不正确的.
任何的想法?
在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)
如何重新绑定操作?
考虑的情况下进行的其他内部的
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是否引入了一种机制来执行此操作.