小编Ale*_*tov的帖子

是否有C++类通过排列实现操作?

是否有C++模板类通过排列和置换组实现操作?这类必须实现查找产品和逆,乘法等.

c++ math permutation algebra

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

是否指向成员特征或类似的东西?

根据我的其他问题.

请考虑以下代码

template<typename T, int N>
struct A {
  typedef T value_type; // save T to value_type
  static const int size = N; // save N to size
};
Run Code Online (Sandbox Code Playgroud)

看,我可以使用value_typesize作为模板参数.

typedef A<int, 2> A1;
typedef A<A1::value_type, A1::size + 3> A2;  // OK,  A2 is A<int,5>
Run Code Online (Sandbox Code Playgroud)

现在我想用指向成员的指针做同样的事情:

struct Foo {
    int m;
    int r;
};

template<int Foo::*Mem>
struct B {
   static int Foo::* const mp;
};

template<int Foo::*Mem>
int Foo::* const B<Mem>::mp = Mem; // …
Run Code Online (Sandbox Code Playgroud)

c++ templates pointer-to-member

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

帮助类型特征

假设我们有以下模板类

template<typename T> class Wrap { /* ... */ };
Run Code Online (Sandbox Code Playgroud)

我们无法改变 Wrap.这很重要.

让我们有来自的类Wrap<T>.例如,

class NewInt  : public Wrap<int>     { /* ... */ };
class MyClass : public Wrap<myclass> { /* ... */ };
class Foo     : public Wrap<Bar>     { /* ... */ };
Run Code Online (Sandbox Code Playgroud)

我们也不能改变这些类.以上所有课程均为第三方.他们不是我的.

我需要以下编译时间type_traits:

template<class T>
struct is_derived_from_Wrap { 
     static const bool value = /* */;
};
Run Code Online (Sandbox Code Playgroud)

我需要什么?

assert(is_derived_from_Wrap<Int>::value == true);  // Indeed I need static assert
assert(is_derived_from_Wrap<MyClass>::value == true);
assert(is_derived_from_Wrap<char>::value …
Run Code Online (Sandbox Code Playgroud)

c++ templates specialization compile-time-constant type-traits

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

在初始化列表中调用私有函数的情况下,它是未定义的行为吗?

请考虑以下代码:

struct Calc
{
   Calc(const Arg1 & arg1, const Arg2 & arg2, /* */ const ArgN & argn) :
      arg1(arg1), arg2(arg2), /* */ argn(argn), 
      coef1(get_coef1()), coef2(get_coef2()) 
   {
   }

   int Calc1();
   int Calc2();
   int Calc3();

private:
  const Arg1 & arg1;
  const Arg2 & arg2;
  // ...
  const ArgN & argn;

  const int coef1; // I want to use const because 
  const int coef2; //      no modification is needed.

  int get_coef1() const {
     // calc coef1 using arg1, arg2, ..., argn;
     // …
Run Code Online (Sandbox Code Playgroud)

c++ constructor const undefined-behavior

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

如何创建指向可变成员的指针?

请考虑以下代码:

struct Foo
{
    mutable int m;

    template<int Foo::* member> 
    void change_member() const {
        this->*member = 12; // Error: you cannot assign to a variable that is const
    }

    void g() const {
    change_member<&Foo::m>();
    }
};
Run Code Online (Sandbox Code Playgroud)

编译器生成错误消息.问题是成员m是可变的,因此可以改变m.但是函数签名隐藏了可变的声明.

如何对指向mutable-member的decalre来编译这段代码?如果不可能,请链接到标准C++.

c++ templates const mutable pointer-to-member

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

我应该使用什么类型的迭代器差异来消除“可能丢失数据”警告?

我需要 x64 模式下警告的通用规则。哪种方式更好?

考虑以下几行代码

const int N = std::max_element(cont.begin(), cont.end()) - cont.begin();
Run Code Online (Sandbox Code Playgroud)

或者

const int ARR_SIZE = 1024;
char arr[ARR_SIZE];
//...
const int N = std::max_element(arr, arr + ARR_SIZE) - arr;
Run Code Online (Sandbox Code Playgroud)

这是我常用的代码。我用 x86 没有任何问题。

但如果我在 x64 模式下运行编译器,我会收到一些警告:

conversion from 'std::_Array_iterator<_Ty,_Size>::difference_type' to 'int', possible loss of data
conversion from '__int64' to 'int', possible loss of data
Run Code Online (Sandbox Code Playgroud)

我想通过共同规则来解决这些问题。哪种方式更好?

  1. 制作static_cast

    const int N = static_cast<int>(
         std::max_element(cont.begin(), cont.end()) - cont.begin()  );
    
    Run Code Online (Sandbox Code Playgroud)

    我认为这不是通用的。而且字母太多。

  2. 将输出类型替换为ptrdiff_t

    const ptrdiff_t N = std::max_element(cont.begin(), cont.end()) - …
    Run Code Online (Sandbox Code Playgroud)

c++ 64-bit x86 warnings visual-studio-2010

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

浮点舍入错误

我不明白以下程序的输出:

int main()
{
    float  x     = 14.567729f;
    float  sqr   = x * x;
    float  diff1 = sqr - x * x;
    double diff2 = double(sqr) - double(x) * double(x);
    std::cout << diff1 << std::endl;
    std::cout << diff2 << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

6.63225e-006
6.63225e-006
Run Code Online (Sandbox Code Playgroud)

我用的是VS2010,x86编译器.

我期望获得不同的输出

0
6.63225e-006
Run Code Online (Sandbox Code Playgroud)

为什么diff1不等于0?要计算sqr - x * x编译器,浮点精度会增加一倍.为什么?

c++ floating-point double ieee-754

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

如何找到所有表达式"variable = variable;" 在Visual Studio 2008中?

我想variable = variable;在源文件中找到所有表达式.我使用Visual Studio 2008.

variable是任何可变的,例如x,i,k123,incr15.

样品:

x = x;           // Should find
x = y;           // No match
ss12 = ss12;     // Should find
ss12 = ss12 + 1; // No match
Run Code Online (Sandbox Code Playgroud)

c# regex visual-studio-2008

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

试图查看vector <string>在map <string,string>中是否没有值

只是为了好玩,我试图用带有boost :: bind的std :: find_if编写一行,以检查地图中向量中给出的所有键是否都没有值,但实际上无法提供一个整齐的代码行.
这是我的尝试

vector<string> v;  
v.push_back("a");  
v.push_back("2");  
...  
map<string, string> m;  
m.insert("b","f");  
...  
std::find_if(v.begin(), v.end(), boost::bind(&string::empty, boost::bind(&map<string,String>::operator[], _1), _2 )) != v.end();  
Run Code Online (Sandbox Code Playgroud)

显然这是一个很大的失败......有人试过这样的事吗?

c++ boost bind

3
推荐指数
2
解决办法
231
查看次数

为什么编译器会生成错误?

为什么编译器会生成错误?

template<class T>
void ignore (const T &) {}

void f() {
   ignore(std::endl);
}
Run Code Online (Sandbox Code Playgroud)

编译器VS2008给出以下错误:cannot deduce template argument as function argument is ambiguous.

c++ templates stl

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

是否可以使用RA-iterator超出范围?

请考虑以下代码:

typedef std::vector<int> cont_t; // Any container with RA-iterators
typedef cont_t::const_iterator citer_t; // Random access iterator

cont_t v(100);
const int start = 15; // start > 0.
citer_t it = v.begin() - start; // Do not use *it

int a1 = 20, b1 = 30; // a1, b1 >= start
int a2 = 30, b2 = 40; // a2, b2 >= start

int x = std::min_element(it + a1, it + b1); // 
int y = std::min_element(it + a2, it + …
Run Code Online (Sandbox Code Playgroud)

c++ iterator stl

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