小编Bak*_*kot的帖子

(g ++ 4.7.1)用等效的类typedef替换显式类型名称无法编译

我正在尝试创建一个模板函数,它接受一个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)

c++ templates g++ c++11

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

Vim - 在 vi​​mscript 变量中写入文件名

我正在做类似的事情

: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吗?

vim

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

声明后使用带有成员函数定义的decltype

我使用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.是什么赋予了?

c++ c++11

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

标签 统计

c++ ×2

c++11 ×2

g++ ×1

templates ×1

vim ×1