我想要一个只返回模板类型的函数,但不接受任何参数.但我的
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) 我有一个类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_ptr在A做出一个模板递归包装来产生new TensorMap,但是那不是我想要的.
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解析数组的值吗?
我正在尝试在我的 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 有一些我不明白的地方。
以下代码无法编译:
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告诉我
Run Code Online (Sandbox Code Playgroud)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'
但以下编译:
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()在第一个代码示例中保留签名但实现我尝试的内容? …
我还没有找到为什么这段代码不起作用:
#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)
Run Code Online (Sandbox Code Playgroud)test.cc:9:5: error: no matching function for call to object of type 'const function<void (int &)>'
是否有通过模板元编程计算连续数之和的智能方法?这将是例如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) 假设我有一个函数来计算一对点之间的欧几里德距离.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
返回前向(通用)引用的最自然方式是什么?
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) 假设我定义了两个变量x和y:
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)
但是,如果我总结的地址一起x和y:
cout << &x + &y << endl;
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
Run Code Online (Sandbox Code Playgroud)invalid operands of types 'int*' and 'int*' to binary 'operator+' cout << &x + &y << endl; ~~~^~~~
为什么我不能添加这两个地址?