小编Yak*_*ont的帖子

C++模板返回参数

我想要一个只返回模板类型的函数,但不接受任何参数.但我的

template <typename T>
T getSomeT() {
  T some;
  return some;
}
Run Code Online (Sandbox Code Playgroud)

没用,它说" error: no matching function for call to 'getSomeT'"和" note: candidate template ignored: couldn't infer template argument 'T'"

但是如果我在其中添加一些伪模板参数,它的工作正常:

template <typename T>
T getSomeT(T fake) {
  T some;
  return some;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates

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

解压缩到可变参数构造函数

我有一个类TensorMap(eigen3库),它采用(简化在这里)数字列表:

class TensorMap{
public:
   template<typename... T>
   TensorMap(T&...i){}
} ;
Run Code Online (Sandbox Code Playgroud)

和使用此类型的结构A.

struct A{
    template<unsigned int N>
    A( NumberList<N> & idx ): m( /* idx(0),idx(1), ...., idx(N-1) */ ) ) {}

    TensorMap m;
};
Run Code Online (Sandbox Code Playgroud)

如何将数字列表注入NumberList<N> & idx到可变参数构造函数中.号码访问idx是由operator()(int i).这有可能通过一些漂亮的模板递归吗?到目前为止,我无法想出一种方法来注入这个,嗯......

当然,人们可以使用一些std::unique_ptrA做出一个模板递归包装来产生new TensorMap,但是那不是我想要的.

c++ variadic-templates c++11 c++14

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

如何用C++解析json数组?

std::stringstream ss;
ss << "{ \"values\": \"A\": 1, \"B\": 10 }";
Run Code Online (Sandbox Code Playgroud)

我想在下面将此流格式化为此格式.

{
  "values": [
    { "A": 1, "B": 10 }
    ...
  ]
}
Run Code Online (Sandbox Code Playgroud)

有人知道如何使用c ++和boost ptree解析数组的值吗?

c++ json boost

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

如何将 std::bind 与 lambda 一起使用

我正在尝试在我的 lambda 中使用 std::bind :

#include <functional>
#include <iostream>
#include <string>

struct Foo {
    Foo() {}
    void func(std::string input)
    {
        std::cout << input << '\n';
    }

    void bind()
    {
        std::cout << "bind attempt" << '\n';
        auto f_sayHello = [this](std::string input) {std::bind(&Foo::func, this, input);};
        f_sayHello("say hello");
    }
};

int main()
{
    Foo foo;
    foo.bind();    
}
Run Code Online (Sandbox Code Playgroud)

当我运行这段代码时,我所期望的是看到以下输出

bind attempt
say hello
Run Code Online (Sandbox Code Playgroud)

但我只看到“绑定尝试”。我很确定 lambda 有一些我不明白的地方。

c++ lambda std stdbind c++11

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

Lambda捕获列表和定义的函数指针类型

以下代码无法编译:

typedef void(*RunnableFun)(int);  //pointer-to-function type

void foo(RunnableFun f) {
}
void bar(const std::string& a) {
    foo([&](int) -> void { std::cout << a; });
}
Run Code Online (Sandbox Code Playgroud)

和IntelliSense告诉我

no suitable conversion function from "lambda []void (int)->void" to "RunnableFun" exists
Run Code Online (Sandbox Code Playgroud)

并且编译器在抱怨

  'void foo(RunnableFun)' : cannot convert argument 1 from 'bar::<lambda_796873cf40a6be4e411eb9df14f486bf>' to 'RunnableFun'
Run Code Online (Sandbox Code Playgroud)

但以下编译:

typedef void(*RunnableFun)(int);  //pointer-to-function type

void foo(RunnableFun f) {

}
void bar(const std::string&) {
    // Notice the empty capture list
    foo([](int) -> void { std::cout << "dummy"; });
}
Run Code Online (Sandbox Code Playgroud)

如何foo()在第一个代码示例中保留签名但实现我尝试的内容? …

c++ lambda

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

如何将lambda传递给lambda?

我还没有找到为什么这段代码不起作用:

#include <iostream>
#include <functional>

using namespace std;

int main()
{
  auto xClosure = [](const function<void(int&)>& myFunction) {
    myFunction(10);};

  xClosure([]
       (int& number) -> void
       {cout<<number<<endl;
       });
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

它返回:

g++ test.cc -o test -std=c++14
Run Code Online (Sandbox Code Playgroud)
 test.cc:9:5: error: no matching function for call to object of type 'const function<void
  (int &)>'
Run Code Online (Sandbox Code Playgroud)

c++ lambda c++11

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

模板元编程:1到n的总和

是否有通过模板元编程计算连续数之和的智能方法?这将是例如1到100的非模板算法.

int i = 0;
for (int n = 1; n <= 100; n++)
  i += n;

return i;
Run Code Online (Sandbox Code Playgroud)

我想过使用一个可变参数添加函数并用参数列表填充它.但是我不确定如何创建参数列表.

// add function
template<typename T>
T add(T a) {
  return a;
}

template<typename T, typename... Args>
T add(T a, Args... args) {
  return a + add(args...); // a + a + a + .. + add()
}
Run Code Online (Sandbox Code Playgroud)

c++ template-meta-programming c++11

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

如何在c ++中处理-nan输出

假设我有一个函数来计算一对点之间的欧几里德距离.point并且point_pair是两个结构定义为:

struct point {
    int x, y;
}
Run Code Online (Sandbox Code Playgroud)

struct point_pair {
    point a, b;
}
Run Code Online (Sandbox Code Playgroud)

以下函数计算将一对点作为输入的距离:

double calc_distance(point_pair pair)
{
  return (sqrt((pair.a.x - pair.b.x) * (pair.a.x - pair.b.x) + (pair.a.y - pair.b.y) * (pair.a.y - pair.b.y)));
}
Run Code Online (Sandbox Code Playgroud)

该函数适用于小点对值; 但对于点对,例如:

651760491 595516649
716636914 955747792
Run Code Online (Sandbox Code Playgroud)

输出是 -nan

我不知道如何解决这个问题,还有什么我应该用来代替双重的吗?

以下是整个代码:https://pastebin.com/5XEr9bTD

c++ c++11

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

如何返回转发引用?

返回前向(通用)引用的最自然方式是什么?

struct B{
         template<class Ar> friend
/*   1*/ Ar&& operator<<(Ar&& ar, A const& a){...; return std::forward<Archive>(ar);}
         template<class Ar> friend 
/*OR 2*/ Ar& operator<<(Ar&& ar, A const& a){...; return ar;}
         template<class Ar> friend 
/*OR 3*/ Ar  operator<<(Ar&& ar, A const& a){...; return std::forward<Ar>(ar);}
         template<class Ar> friend 
/*OR 4*/ Ar  operator<<(Ar&& ar, A const& a){...; return ar;}
/*OR 5*/ other??
}
Run Code Online (Sandbox Code Playgroud)

我想到的情况是一个类似流的对象,它被构造并立即使用.例如:

dosomethign( myarchive_type{} << B{} << B{} << other_unrelated_type{} );
Run Code Online (Sandbox Code Playgroud)

c++ return c++11 forwarding-reference

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

为什么不能将两个地址加在一起?

假设我定义了两个变量xy

int x = 5;
int y = 6;
Run Code Online (Sandbox Code Playgroud)

我可以x通过以下方式获取存储地址:

cout << &x << endl; // 0x61fe18
Run Code Online (Sandbox Code Playgroud)

这是一个十六进制数。现在,如果我想在加上 5 后打印这个数字,这很简单:

cout << &x + 5 << endl; // 0x61fe1d
Run Code Online (Sandbox Code Playgroud)

但是,如果我总结的地址一起xy

cout << &x + &y << endl;
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

invalid operands of types 'int*' and 'int*' to binary 'operator+'
cout << &x + &y << endl;
        ~~~^~~~
Run Code Online (Sandbox Code Playgroud)

为什么我不能添加这两个地址?

c++ pointers integer-arithmetic

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