- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
test1.cpp 编译,但 test2.cpp 不编译。两者之间的唯一区别是我在 test1.cpp 中的类声明中定义了 Handle::add_it
,但在 test2.cpp 中定义了类声明.
test1.cpp:g++ test1.cpp -o test1 -std=c++11
#include <iostream>
template<typename B>
class Handle
{
public:
decltype(B.operator(int)) add_it(int x)
{
return b(x);
}
B b;
};
struct Add1
{
int operator()(int x)
{
return x + 1;
}
};
int main()
{
Handle<Add1> h;
std::cout << h.add_it(5) << std::endl;
}
test2.cpp:g++ test2.cpp -o test2 -std=c++11
#include <iostream>
template<typename B>
class Handle
{
public:
decltype(B.operator(int)) add_it(int x);
B b;
};
template<typename B>
decltype(B.operator(int)) Handle<B>::add_it(int x)
{
return b(x);
}
struct Add1
{
int operator()(int x)
{
return x + 1;
}
};
int main()
{
Handle<Add1> h;
std::cout << h.add_it(5) << std::endl;
}
错误
test2.cpp:13:11: error: expected primary-expression before ‘.’ token
decltype(B.operator(int))
^
test2.cpp:13:20: error: expected type-specifier before ‘(’ token
decltype(B.operator(int))
^
test2.cpp:13:21: error: expected primary-expression before ‘int’
decltype(B.operator(int))
最佳答案
您可以使用 std::declval
修改它:
template<typename B>
class Handle
{
public:
decltype(std::declval<B>()(int())) add_it(int x) {
return b(x);
}
B b;
};
或者在类的定义之外:
template<typename B>
class Handle {
public:
decltype(std::declval<B>()(int())) add_it(int x);
B b;
};
template<typename B>
decltype(std::declval<B>()(int())) Handle<B>::add_it(int x) {
return b(x);
}
关于c++ - 带有模板参数的 decltype,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34740248/
由 this question 触发,我想知道是否允许这样做: template T foo(){return T{};} struct bar {}; int main() { bar a
我正在尝试以一种通用的方式实现 group_by 方法,我可能已经实现了它(除了它不适用于 C 数组),但代码对我来说仍然很难看...... 有没有更简单的方法来做我想做的事(+让它适用于所有容器和
这个问题在这里已经有了答案: What is decltype with two arguments? (2 个答案) 关闭 7 年前。 我遇到了一个decltype(),有两个参数作为模板函数的返
这是问题的后续:What does the void() in decltype(void()) mean exactly? . decltype(void()) 编译得很好,void() 在这种情况
这是我第一次使用decltype,我不太确定我是否正确使用它。该代码确实编译并且似乎适用于像 char 和 int 这样的 POD。 但是,我想知道我是否会遇到更复杂的数据类型的任何问题 - 其他人警
我一直在使用解析为与声明相同类型的定义中的推导返回类型。这有效: template struct Cls { static std::size_t f(); }; template declt
我发现它们是不同的,并且语言标准规定了每个语句应该检索什么样的类型(变量和表达式之间的差异)。但我真的很想知道为什么这两种类型应该不同? #include int x=0; decltype((x))
关于 decltype(x) 和 decltype((x)) 之间的区别,我已经读过很多遍了。一个例子如下。 x is the name of a variable, so decltype(x) i
我有一个简单的模板化包装器结构,其成员函数在其模板类型的对象上调用 .error()。 template struct Wrapper { T t; decltype(auto) f
decltype(auto) 和 decltype(returning expression) 作为函数(模板)的返回类型 if expr 有什么区别在这两种情况下都使用不带括号? auto f()
我在想 decltype((x)) 给出了 & 引用类型,但是一些实验表明还有其他事情: #include int main(){ int x = 0; decltype((x)) r
例如,简单的恒等仿函数: template class identity { public: constexpr auto operator ()(T && i) -> decltype(s
这个问题在这里已经有了答案: 关闭10 年前。 Possible Duplicate: decltype and parenthesis 我在维基百科上找到了这个: auto c = 0;
// g++ 7.3 template struct td; int main() { int a = 1; td t1; td t2; return 0; } 编译结果如下: 错误:
这确实是一个 C++14 问题。而且它的理论性多于实践性。 有时您会零碎地构建一个函数的结果: int f( int x, int y ) { int a; //... re
我试图检测成员函数 baz() 的存在在模板参数中: template struct ImplementsBaz : public std::false_type { }; template stru
考虑以下代码:(Ideone) struct S { int a() {return 0;} decltype(a()) b() {return 1;} }; 它给了我以下错误: er
(如果您是 C++11 专业人士,请跳至粗体段落。) 假设我想编写一个模板方法,该方法调用并返回传递的对象的结果,该对象的类型是模板参数: template ReturnType doSomethin
代码 #include int main() { int a=3; int *p=&a; decltype (a) k1; decltype (*p) k2;
我是 C++ 新手。我正在尝试学习 decltype 的概念。我在网上看到这段代码。我将 decltype(s1.size()) 更改为 int,代码工作正常。 decltype(s1.size())
我是一名优秀的程序员,十分优秀!