小编Ama*_*eus的帖子

模板专业化和继承

假设我有一个包含许多函数的模板类,我想专门化它们只更改其中的一些,并保持其他完全按照基本模板类中的指定.

我怎样才能做到这一点?

下面是我想要实现的,但解决方案并不好,因为它不允许我引用intas的特化Base<int>- 我需要使用IntSpec它.

#include <iostream>

using namespace std;

template<typename T>
struct Base
{
  void print1() {cout << "Base::print1" << endl;};
  void print2() {cout << "Base::print2" << endl;};
};

struct IntSpec : public Base<int>
{
  void print2() {cout << "Base<int>::print2()" << endl;};
};

int main()
{
  Base<double> d;
  // Base<int> i;  <-- I want this kind of instantiation
  IntSpec i;

  d.print1();
  d.print2();
  i.print1();
  i.print2();
}
Run Code Online (Sandbox Code Playgroud)

输出是:

Base::print1
Base::print2
Base::print1
Base<int>::print2()
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates c++11

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

Lambda:可以悬挂的副引用捕获

有效的现代C++中的Scott Meyers在lambda章节中说:

请考虑以下代码:

void addDivisorFilter()
{
    auto calc1 = computeSomeValue1();
    auto calc2 = computeSomeValue2();

    auto divisor = computeDivisor(calc1, calc2);

    filters.emplace_back(
          [&](int value) { return value % divisor == 0; }
    );
}
Run Code Online (Sandbox Code Playgroud)

这段代码是一个等待发生的问题.lambda引用局部变量divisor,但addDivisorFilter返回时该变量不再存在.这是在filters.emplace_back返回后立即进行的,因此添加的功能filters在到达时基本上是死的.使用该过滤器几乎从创建时开始产生未定义的行为.

问题是:为什么它是一个未定义的行为?据我所知,filters.emplace_back只有在lambda表达式完成后才返回,并且在执行期间,它divisor是有效的.

更新

我错过的重要数据包括:

using FilterContainer = std::vector<std::function<bool(int)>>;
FilterContainer filters;
Run Code Online (Sandbox Code Playgroud)

c++ lambda c++11 c++14

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

为什么foo((&i)++)给出Lvalue所需的错误.没有关联的数组

我理解Lvalue的一些事情,但我不明白下面的代码是如何产生错误的:

#include<stdio.h>

void foo(int *);

int main() 
{
   int i=10;
   foo((&i)++);
}

void foo(int *p)
{
    printf("%d",*p);
}
Run Code Online (Sandbox Code Playgroud)

6:13:错误:左值作为增量操作数foo((&i)++); ^

c reference lvalue

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

为什么我不能用C++中的单个char构造一个std :: string?

std::string从a 做一个char.例如,不会编译,

#include <string>

int main()
{
    char c = 'a';
    std::string s(c);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在g ++中,我得到了这么多错误,

foo.cpp: In function 'int main()':
foo.cpp:6:20: error: no matching function for call to 'std::__cxx11::basic_strin
g<char>::basic_string(char&)'
     std::string s(c);
                    ^
In file included from i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/in
clude/c++/string:52:0,
                 from foo.cpp:1:
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:535:9: note: candidate: template<class _InputIterator> std::__cxx11::bas
ic_string<_CharT, _Traits, _Alloc>::basic_string(_InputIterator, _InputIterator,
 const _Alloc&)
         basic_string(_InputIterator __beg, _InputIterator __end,
         ^
i:/Qtandroid/Qt5.7.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/basic_s
tring.h:535:9: note:   template argument deduction/substitution failed:
foo.cpp:6:20: note:   candidate expects 3 arguments, …
Run Code Online (Sandbox Code Playgroud)

c++ stdstring c++11

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

对于使用虚拟接口对象的范围(c ++ 11)

我正在编程接口,我想以一种通用的方式对其内容进行交互.所以,一般来说,我的接口都有这样的原型:

class Interface
{
public:
  class Iterator;
  virtual Interface::Iterator* begin() = 0;
  virtual Interface::Iterator* end() = 0;  

  class Iterator
  {
  public:
    virtual const Iterator* operator++() = 0;
    virtual bool operator!=(const Iterator& i) = 0;
  };
};
Run Code Online (Sandbox Code Playgroud)

和吼叫,是一个简单的专业化的例子:

class Derived : public Interface
{
public:
  Derived() : array {2, 0, 1, 5, 4, 3} {};
  Iterator* begin() { return new Derived::IterDerived(0);};
  Iterator* end() { return new Derived::IterDerived(6);};

  int array[6];

public:
  class IterDerived : public Interface::Iterator
  {
  public:
    IterDerived(int i) {pos = …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

功能指针

这个错误意味着什么?为什么当我将所有类型都加倍时,它不会显示相同的错误?

C2556'int div(int,int)':重载函数的区别仅在于'div_t div(int,int)'返回类型'C2371'div
':redefinition; 不同的基本类型
C2491'div':不允许定义dllimport函数
C2664'int calculate(int,int,int(__ cdecl*)(int,int))':不能将参数3从'overloaded-function'转换为'int( __cdecl*)(int,int)'


#include <iostream>
using namespace std;

int sum(int x, int y) {
    return x + y;
}

int subs(int x, int y) {
    return x - y;
}

int mult(int x, int y) {
    return x * y;
}

int div(int x, int y) {
    return x / y;
}
int calculate(int x, int y, int(*func)(int, int)) {
    return func(x, y);
}
void main() {
    cout<<"Sum:"<< calculate(8, 4, sum)<<endl;
    cout …
Run Code Online (Sandbox Code Playgroud)

c++ pointers function

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

在编译时确保调用方法

如何在编译时确保调用特定方法?

例如,假设我有一个包含2个方法的对象:

struct Foo
{
   ... func1(...);
   ... func2(...);
};
Run Code Online (Sandbox Code Playgroud)

我想确保在调用func2之前调用func1,即:

int main()
{
   Foo f;
   ...
   f.func1(...);
   f.func2(...);
   f.func2(...); // and so on
}
Run Code Online (Sandbox Code Playgroud)

但是如果我这样做的话,我想生成一个编译错误:

int main()
{
   Foo f;
   ...
   f.func2(...);  // generate a compile error due the fact that func1 must be called first
   f.func1(...);
   f.func2(...); // and so on
}
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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

标签 统计

c++ ×6

c++11 ×5

c ×1

c++14 ×1

function ×1

inheritance ×1

lambda ×1

lvalue ×1

pointers ×1

reference ×1

stdstring ×1

templates ×1