我正在尝试创建一个模板函数,它接受一个iterable和一个函数,这样传递的函数将被隐式地转换为std::function适当的类型(因此允许它与完整函数和lambdas一起使用).
这是代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <typeinfo>
template<typename T>
void bar(const T & base, std::function<bool(int)> f) // works
//void bar(const T & base, std::function<bool(typename T::iterator::value_type)> f) // fails to compile
{
std::cout << ((typeid(std::function<bool(int)>) == typeid(std::function<bool(typename T::iterator::value_type)>))?"identical":"distinct") << std::endl;
}
bool filter(int x) { return x%2==0; }
int main() { bar(std::vector<int> {0, 1}, filter); }
Run Code Online (Sandbox Code Playgroud)
用g++-4.7 -std=c++11 -o itest itest.cpp这个编译产生identical.
如果取消注释第10行和注释第9行并按上面编译,则编译失败
g++-4.7 -std=c++11 -Wall -Werror -o itest itest.cpp
itest.cpp: In function …Run Code Online (Sandbox Code Playgroud) 我正在做类似的事情
:let foo="bar"
:echom foo
bar
:w foo
"foo" [New File] 0 lines, 0 characters written
Run Code Online (Sandbox Code Playgroud)
我期待/希望编写一个名为“bar”的文件,而不是一个名为“foo”的文件。假设我有一个字符串存储在变量“foo”中,如何将当前缓冲区写入名称为该字符串的文件?
顺便说一句,有人可以解释一下什么:w foo和:echom foo正在做的不同foo吗?
我使用decltype作为成员函数的返回类型,但定义和声明不匹配.这是一些代码:
template<typename T>
struct A {
T x;
auto f() -> decltype(x);
};
template<typename T>
auto A<T>::f() -> decltype(x) {
return this->x;
}
int main() {}
Run Code Online (Sandbox Code Playgroud)
这产生了
test.cc:10:6: error: prototype for 'decltype (((A<T>*)0)->A<T>::x) A<T>::f()' does not match any in class 'A<T>'
test.cc:6:7: error: candidate is: decltype (((A<T>*)this)->A<T>::x) A<T>::f()
Run Code Online (Sandbox Code Playgroud)
不同之处在于定义具有(A<T>*)0声明的位置(A<T>*)this.是什么赋予了?