我试图实现一个模板对类,它使用sfinae来区分数组类型和非数组类型。到目前为止,我有以下代码:
template <typename T>
class pair
{
public:
//default constructors. one for non-array types and one for arrays. These work.
template <typename temp_type = T>
pair(typename boost::disable_if<boost::is_array<temp_type>>:
我尝试编写一个通过索引访问容器中的元素的类。因此,我将能够重新路由指向的容器(但这对于我当前的问题并不重要)。它将帮助我摆脱复杂的复制构造函数。
这个类应该表现为一个数字(即索引)和一个指针(即它所指向的指针)。因此,我有转换。然而,我得到了编译器错误:
In instantiation of 'class paxRef<Dog&, std::vector<Dog> >':
34:47: required from here
13:5: error: forming pointer to reference type 'Dog&
我正在尝试学习boost::bind,boost::lambda库,以及如何将它们与STL算法一起使用。假设我有按int key排序的int-string对的向量。然后,在保持向量排序的同时插入新对的位置可以如下所示:
std::vector<std::pair<int, string> > entries;
...
int k = ...;
// Let's ignore std::lower_bound return value for now
std::lower_bound (entries.begin(), entries.end(), k,
考虑以下程序:
#include <iostream>
#include <iterator>
#include <vector>
#include <utility>
using namespace std; //just for convenience, illustration only
typedef pair<int, int> point; //this is my specialization of pair. I call it point
istream& operator >> (istre
我一直在使用C++中的函数式函数。特别是,我有一个对的向量,我想按对中的第一个元素进行排序。我开始编写一个完全专门化的函数器(例如,类似于"bool MyLessThan(MyPair &lhs,MyPair &rhs)")。然后,仅仅因为这类东西很有趣,我就想尝试编写一个通用的"Apply F to the first element of this pair“函数器。我写了下面的内容,但是g++不喜欢。我得到了:
错误:“template struct Pair1stFunc2”的模板参数列表中参数2处的类型/值不匹配。错误:应为类型,但得到的是“l
int main() {
using kv = std::pair<const int ,int>;
std::vector<kv> vec;
kv a{1,1};
vec.insert(vec.begin(),a);
}
我试图将元素插入到该向量中,但编译器给出了这个eerror:
cannot assign to non-static data member 'first' with const-qualified type 'const int'
而push_back()将编译correctly.Why,是吗?在这
问题是:从It类继承(并覆盖方法)的ExampleIt,所以当我重载Wrapped类中的运算符时(它从It调用一些方法,它应该被ExampleIt覆盖)。
我们想要的效果是,当我重载operator*时,我应该能够调用*name_of_Wrapped_class,这应该执行虚方法dereference (来自It),它应该被ExampleIt覆盖。
class It {
public:
virtual std::pair<int, std::string> dereference() const;
};
class ExampleIt : It {
public:
我在试着让操作员超负荷。我注意到,在执行长表达式时,编译器会在语句末尾(在;之后)创建临时代码并销毁它们。但是,当表达式变长或对象更大时,内存就会无缘无故地被占用(据我所知)。我想要的是像这样使用后销毁它们:
Test m3 = m1 - m2*2 + 3*m1;
/* Temporaries predicted from result, temporaries might
have different declarations like 'const Test temp = ...;'
Test temp_m1 = m1*3;
Tes
我有一个关于std::function的例子
int add(int x, int y, int z) {return x + y + z;}
int add(int a, int b) {return a + b;}
int main()
{
std::function<int(int, int)> fn = add; // error
int(*pfn)(int, int) = add; // OK
fn = pfn; // ok fn is bound to add(int, int)
std::cout << fn(5, 7
正如预期的那样,下面的代码是。
#include <type_traits>
#include <utility>
int main()
{
using T = std::pair<const int, int>;
const auto ok = std::is_assignable<T, T>::value; // true
T x;
T y;
x = y; // compiler error
}
但是ok 的值与以下三个编译器一起使用。
g++ (Ubuntu5.4.0-6 ubuntu1~16.0
我不明白这段代码中的错误是什么:
#include <set>
#include <utility>
#include <iostream>
using namespace std;
class A
{
public:
A(unsigned int a) : _a(a) { }
A() : _a(0) { }
unsigned int a() const { return _a; }
private:
unsigned int _a;
};
class B
{
pu