小编Ayj*_*jay的帖子

防止非常量左值解析为右值引用而不是const左值引用

我无法通过const引用重载函数来获取值,或者如果它是rvalue则是rvalue引用.问题是我的非const左值绑定到函数的右值版本.我在VC2010中这样做.

#include <iostream>
#include <vector>

using namespace std;

template <class T>
void foo(const T& t)
{cout << "void foo(const T&)" << endl;}

template <class T>
void foo(T&& t)
{cout << "void foo(T&&)" << endl;}

int main()
{
    vector<int> x;
    foo(x); // void foo(T&&) ?????
    foo(vector<int>()); // void foo(T&&)
}
Run Code Online (Sandbox Code Playgroud)

优先级似乎是推导foo(x)为

foo< vector<int> & >(vector<int>& && t)
Run Code Online (Sandbox Code Playgroud)

代替

foo< vector<int> >(const vector<int>& t)
Run Code Online (Sandbox Code Playgroud)

我尝试用r替换rvalue-reference版本

void foo(typename remove_reference<T>::type&& t)
Run Code Online (Sandbox Code Playgroud)

但这只会导致所有内容都解析为const-lvalue引用版本.

我该如何防止这种行为?为什么这仍然是默认值 - 考虑到允许修改rvalue-references似乎很危险,这给我留下了意外修改的局部变量.

编辑:刚添加了非模板版本的函数,它们按预期工作.将函数设为模板会更改重载决策规则吗?那真是令人沮丧!

void bar(const vector<int>& t)
{cout << "void bar(const …
Run Code Online (Sandbox Code Playgroud)

c++ templates metaprogramming rvalue-reference

19
推荐指数
2
解决办法
1902
查看次数

为什么需要对ForwardIterators建模DefaultConstructible?

我似乎无法找到任何标准算法来证明默认构造a的要求ForwardIterator.

是否有任何实际原因,或者我可以安全地忽略它?

c++ iterator

10
推荐指数
1
解决办法
384
查看次数

在C++ 11中实现干净的lambda函数

我一直在玩新的C++ 11 lambda,并且完全指定模板参数的要求是一个真正的拖累.我的语法喜欢使用是类似于下面的内容:

#include <vector>
#include <algorithm>

struct foo
{
    void bar() {}
};

int main()
{
    vector<foo> v(10);

    for_each(v.begin(), v.end(), [](f) {f.bar();});
                                   ^^^
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以得到任何接近这个的东西吗?Boost的Phoenix库是可以的,但是调用成员函数的语法需要大量的样板 - 我想我是在C++ 11轻松调用成员函数以及Phoenix自动推断类型之后.

目前的想法

我已经了解了这个语法:

vector<foo> x(1);
vector<bar> y(1);
for_each(x.begin(), x.end(), [](_a f) {f->f();});
for_each(y.begin(), y.end(), [](_a b) {b->b();});
Run Code Online (Sandbox Code Playgroud)

哪个有效,但您必须添加每种类型的功能(例如ADD_AUTO_LAMBDA_SUPPORT(foo);).它还有一个限制,即所有支持的类型都不能有任何不明确的成员.

完整的代码是:

#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

struct foo
{
    foo() : x(3) {}
    int x;
    void f() { cout << x << endl;}
}; …
Run Code Online (Sandbox Code Playgroud)

c++ lambda templates c++11

9
推荐指数
2
解决办法
4204
查看次数

绑定到List &lt;string&gt;的ComboBox的MemberValue是什么?

ValueMambera ComboBox绑定的a是List<string>什么?

我正在使用Windows窗体和.NET Framework 4。

  cmbForms.DataSource = Forms;
  cmbForms.ValueMember="System.String";
  if (!string.IsNullOrWhiteSpace(PhotoDescription.Details.Form))
  {
      cmbForms.SelectedValue = PhotoDescription.Details.Form;
  }
Run Code Online (Sandbox Code Playgroud)

哪里Forms是:

 public List<string> Forms { get; set; }
Run Code Online (Sandbox Code Playgroud)

c# data-binding combobox list winforms

2
推荐指数
1
解决办法
3075
查看次数

std :: enable_if特化失败

我一直在搞乱enable_if,我似乎偶然发现了一些不一致的行为.这是VS2010.我把它减少到下面的样本.

#include <type_traits>

using namespace std;

// enable_if in initial template definition
template <class T, class Enable = enable_if<true>> struct foo {};

foo<int> a; //OK

// explicit specialisation
template <class T, class Enable = void> struct bar;
template <class T> struct bar<T, void> {};

bar<int> b; //OK

// enable_if based specialisation
template <class T, class Enable = void> struct baz;
template <class T> struct baz<T, std::enable_if<true>> {};

baz<int> c; //error C2079: 'c' uses undefined struct 'baz<T>'
Run Code Online (Sandbox Code Playgroud)

这是代码或编译器中的错误吗?

c++ templates template-specialization enable-if

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