【发布时间】:2014-01-20 22:45:30
【问题描述】:
第 1 行导致 error: conversion from ‘C<void()>’ to non-scalar type ‘C<void (*)()>’ requested。我知道我可以将其写为第 2 行,但我如何使用 make_class() 并将其分配给变量?
#include <iostream>
using namespace std;
template<class T> class C {
T f;
public:
C(T ff) : f(ff) {}
};
template<class Ft> C<Ft> make_class(const Ft& f)
{
return C<Ft>(f);
}
void f()
{
cout << "f()" << endl;
}
int main()
{
// C<void(*)()> v = make_class(f); // line 1
C<void(*)()> v(f); // line 2
return 0;
}
另一个问题来自这个link。代码如下所示。如何理解第 3 行?
template <typename F>
struct foo {
F f;
void call() {
f();
}
};
void function() {
std::cout << "function called" << std::endl;
}
int main() {
foo<void(*)()> a = { function }; // line 3: { } is an array?
a.call();
}
谢谢。
【问题讨论】:
标签: c++