小编Pub*_*bby的帖子

类型在编译时已知的虚方法

如果我这样做:

Dog dog; //class with virtual methods
Cat cat; //class from same base as Dog

dog.eat(); //call virtual method
cat.eat(); //call virtual method
Run Code Online (Sandbox Code Playgroud)

然后eat()s将是正常的方法调用,不会要求v表 - 正确吗?我可以假设它会运行与非虚方法相同吗?

(是的,我知道编译器如何处理虚函数不在标准中 - 我想知道大多数编译器的作用)

c++ methods virtual inheritance

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

sizeof... 是否允许在模板参数中进行专门化?

我正在尝试使用 GCC 4.7 快照做一些类似的事情:

\n\n
template <int n, int... xs>\nstruct foo { \n  static const int value = 0;\n};\n\n// partial specialization where n is number of ints in xs:\n\ntemplate <int... xs>\nstruct foo<sizeof...(xs), xs...> { // error: template argument \xe2\x80\x98sizeof (xs ...)\xe2\x80\x99\n                                   //  involves template parameter(s)\n  static const int value = 1;\n};\n\ntemplate <int... xs>\nstruct foo<sizeof(xs), xs...> { // This compiles fine. sizeof(xs) is sizeof int \n                                // even though packs aren't expanded\n  static const int value = 2;\n};\n
Run Code Online (Sandbox Code Playgroud)\n\n

该错误很奇怪,因为在这种情况下, sizeof …

c++ templates sizeof template-specialization c++11

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

模板类中的类型转换运算符 - 无论显式如何都被调用

我有这个代码.在主要我想要使用类型转换,但使用调试我明白在这一行 ob2=(Point2D<double>)ob1;

template <class T1> Point2D(Point2D<T1>& ob)无论explicit以前调用构造函数,template <class T1> explicit Point2D(Point2D<T1>& ob)为什么会发生这种情况?我希望这operator Point2D<T1>()是被调用的.

template <class T>
class Point2D
{
public:
    T x;
    T y;
    Point2D(T _x=0,T _y=0):x(_x),y(_y)
    {
    }
    Point2D(Point2D& ob)
    {
        x=ob.x;
        y=ob.y;
    }
    template <class T1>
    Point2D(Point2D<T1>& ob)
    {
        x=ob.x;
        y=ob.y;
    }
    template <class T1>
    operator Point2D<T1>()
    {
        return Point2D<T1>(x,y);
    }
};
int main()
{
    Point2D<int> ob1(10,10);
    Point2D<double> ob2(20,20);
    ob2=(Point2D<double>)ob1;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ templates type-conversion

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

如何从实现中提取声明(.hpp文件)(.cpp文件)?

我想知道是否有一个工具(可能是GNU C++编译器)从一个.cpp文件中获取充满函数的.hpp文件,其中包含上述函数声明.

示例:

我有:
magic.cpp

int foo() { return 42; }
char bar() { return 'z'; }
Run Code Online (Sandbox Code Playgroud)

我想在应用这个奇妙的工具后获得这个:
magic.hpp

int foo();
char bar();
Run Code Online (Sandbox Code Playgroud)

c++

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

指向未指定类类型的成员函数指针 - 可能吗?

是否可以声明一个可以指向任何类的成员函数的函数指针(非 C++ 11)(阅读:不是特定的类)?

例如,如果我有类 A、B 和 C。C 中声明了一个函数指针,我想在指向 B 的成员函数之一和 A 的成员函数之一之间切换该指针。C++ 允许这样做吗?

c++ member-function-pointers function-pointers c++03

5
推荐指数
2
解决办法
1424
查看次数

为什么C++虚拟调用并不比非虚拟调用慢?

在我的理解中,对于C++虚拟调用,它需要:

  1. 从符号表中获取对象的类型
  2. 从类型表中获取v表
  3. 使用v表中的函数签名搜索函数
  4. 调用该函数.

而对于非虚拟(例如在C)呼叫,仅需要#4.

我认为#3应该是最耗时的.鉴于C++中实时覆盖的性质,我看不到上述步骤的编译时间优化的可能性.因此,对于具有长函数签名的复杂类继承,C++虚拟调用应该比非虚拟调用慢得多.

但所有的说法都是相反的,为什么?

c++ performance

5
推荐指数
2
解决办法
1020
查看次数

为什么这个ASIO示例使用成员变量来传递状态而不是使用bind?

ASIO HTTP Server 3示例中,有如下代码:

void server::start_accept()
{
  new_connection_.reset(new connection(io_service_, request_handler_));
  acceptor_.async_accept(new_connection_->socket(),
      boost::bind(&server::handle_accept, this,
        boost::asio::placeholders::error));
}

void server::handle_accept(const boost::system::error_code& e)
{
  if (!e)
  {
    new_connection_->start();
  }

  start_accept();
}
Run Code Online (Sandbox Code Playgroud)

从本质上讲,new_connection_是的构件server类和用于传递从一个连接start_accepthandle_accept.现在,我很好奇为什么new_connection_实现为成员变量.

使用bind而不是使用成员变量来传递连接是否也有效?像这样:

void server::start_accept()
{
  std::shared_ptr<connection> new_connection(new connection(io_service_, request_handler_));
  acceptor_.async_accept(new_connection_->socket(),
      boost::bind(&server::handle_accept, this,
        boost::asio::placeholders::error),
        new_connection);
}

void server::handle_accept(boost::system::error_code const& error, std::shared_ptr<connection> new_connection)
{
  if (!error) {
    new_connection->start();
  }
  start_accept();
}
Run Code Online (Sandbox Code Playgroud)

如果是这样,为什么该示例使用成员变量?是否避免涉及复制?

(注意:我对ASIO不满意,所以这里可能存在一个根本的误解)

c++ boost boost-asio

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

用户定义的文字可以将函数作为参数吗?

函数可以与用户定义的文字一起使用吗?

如果是这样,可以做些什么恶作剧?这合法吗?

void operator "" _bar(int (*func)(int)) {
  func(1);
}

int foo(int x) {
  std::cout << x << std::endl;
}

int main() {
  foo(0);    // print 0
  foo_bar;   // print 1
}
Run Code Online (Sandbox Code Playgroud)

c++ user-defined-literals c++11

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

C++显式模板化函数实例化,用于不同类型的模板化类

我试图在类型T的模板化类中显式实例化类型U的模板化函数.下面的代码生成警告,链接器找不到显式实例化ReinterpretAs().任何人都可以发现错误或建议如何做到这一点?我正在使用VC++ 2010.

template<typename T> 
class Matrix
{
  public:
    template<typename U> Matrix<U> ReinterpretAs() const;
};

template<typename T>
template<typename U>
Matrix<U> Matrix<T>::ReinterpretAs() const
{
   Matrix<U> m;
   // ...
   return m;
}


// Explicit instantiation.
template class Matrix<int>;
template class Matrix<float>;

template Matrix<float>  Matrix<int>::ReinterpretAs<float>();
template Matrix<int>    Matrix<float>::ReinterpretAs<int>();
Run Code Online (Sandbox Code Playgroud)

上面的最后两行给出了编译器警告:

warning #536: no instance of function template "Matrix<T>::ReinterpretAs 
[with T=float]" matches the specified type
Run Code Online (Sandbox Code Playgroud)

马克,提前谢谢你

c++ templates visual-c++

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

如何查找malloc调用实际耗尽了多少内存?

如果我打电话:

char *myChar = (char *)malloc(sizeof(char));
Run Code Online (Sandbox Code Playgroud)

我可能会使用超过1个字节的内存,因为malloc可能会自己使用一些内存来跟踪堆中的空闲块,并且它总是可以通过始终沿着某些边界对齐分配来节省一些内存.

我的问题是:有没有办法找出特定malloc调用真正消耗了多少内存,包括有效的对齐成本,以及malloc/ free?使用的开销?

为了清楚起见,我并没有要求在调用之后找出指针指向的内存量malloc.相反,我正在调试一个使用大量内存的程序,我想知道代码的哪些部分正在分配多少内存.我希望能够使内部记忆会计与top报告的数字非常接近.理想情况下,我希望能够以编程方式执行此操作malloc,而不是在检查点获取摘要.

c memory malloc memory-management

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