小编Fab*_*era的帖子

两个超类具有相同名称但不同签名的成员函数时不明确

struct A {
    void f(int x) {}
};

struct B {
    template<typename T> void f(T x) {}
};

struct C : public A, public B {};

struct D {
    void f(int x){}
    template<typename T> void f(T x) {} 
};


int main(int argc, char **argv) {
    C c;
    c.f<int>(3);
    D d;
    d.f<int>(3);
}
Run Code Online (Sandbox Code Playgroud)

呼叫d.f是好的原因是什么,但是c.f给出了

error: request for member ‘f’ is ambiguous
error: candidates are: template<class T> void B::f(T)
error:                 void A::f(int)
Run Code Online (Sandbox Code Playgroud)

c++ templates class-hierarchy

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

为什么pthread_exit抛出省略号捕获的东西?

如果被调用的函数pthread_create具有以下结构

try{
  ...code....
  pthread_detach(pthread_self());
  pthread_exit(NULL);
}catch(...){
  std::cout<<"I am here"<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)

为什么省略号的异常处理程序在执行时被调用pthread_exit?(请注意std::exception,例如,不会抛出)

c++ exception-handling exception pthreads cancellation

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

如何检测类型是否是另一种类型的可见基础?

如果我做

struct A{};
struct C:private A{};

typedef char (&yes)[1];
typedef char (&no)[2];

template <typename B, typename D>
struct Host
{
 operator B*() const;
 operator D*();
};

template <typename B, typename D>
struct is_base_of
{
template <typename T> 
static yes check(D*, T);
static no check(B*, int);

static const bool value = sizeof(check(Host<B,D>(), int())) == sizeof(yes);
};

int main(){
 std::cout<<is_base_of<A,C>::value<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我得到一个1.我想C在私有时获得0,在公共A时获得1 .CA

[代码源自`is_base_of`如何工作的?]

c++ inheritance templates

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

g ++,需要链接器警告/错误才能进行多个模板专业化

想象一下你有一个文件啊

 #include <iostream>

template<typename T> struct A{  
  int magic;
  A():magic(1234){}
  void f(){std::cout<<"default f"<<magic<<std::endl;}
};


void f(A<int>* a);
Run Code Online (Sandbox Code Playgroud)

然后函数f在"a.cpp"中定义

  #include "a.h"
void f(A<int>* a){
  a->f();
}
Run Code Online (Sandbox Code Playgroud)

最后,"main.cpp"专门化模板,然后使用f

#include "a.h"
template<> struct A<int>{   
};

int main(){
  A<int> a;
  f(&a);

}
Run Code Online (Sandbox Code Playgroud)

很明显,编译器使用非专用版本的ao,以及main.o的专用版本,即恰好有两种不同的A实现.执行时,f只能打印垃圾/段错误,因为传递的对象具有不同的结构从预期的.

有没有办法让链接器警告有两个版本的A?

c++ linker templates g++

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

将超类函数作为非类型名模板参数传递

假设你有

  struct A{
  void f(){}
};


struct B:public A{
};


template<typename C,void (C::*f)()>
struct Call{

  void operator()(C* c){
    (c->*f)();
  }
};
Run Code Online (Sandbox Code Playgroud)

为什么

int main(){
  void (B::*f)()=&B::f;    
}
Run Code Online (Sandbox Code Playgroud)

工作但是

 Call<B,&B::f> a;
Run Code Online (Sandbox Code Playgroud)

没有,抱怨

could not convert template argument ‘&A::f’ to ‘void (B::*)()
Run Code Online (Sandbox Code Playgroud)

(Call<A,&A::f>显然有效)

以类似的方式

const void (B::*f)()=&B::f;
Run Code Online (Sandbox Code Playgroud)

cannot convert ‘void (A::*)()’ to ‘const void (B::*)()’ in initialization
Run Code Online (Sandbox Code Playgroud)

c++ templates

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

gcc了解编译时间的位置

我有一个大量使用模板的项目.最近编译时间突然上升.我想知道是否有办法看到哪些类/行需要最多的时间来编译g ++.

以下是-ftime-report的一些输出

Execution times (seconds)
 TOTAL                 :   0.30             0.05             0.37               9119 kB

Execution times (seconds)
 garbage collection    :   0.91 ( 6%) usr   0.00 ( 0%) sys   0.92 ( 5%) wall       0 kB ( 0%) ggc
 callgraph construction:   0.23 ( 2%) usr   0.11 ( 3%) sys   0.37 ( 2%) wall   10652 kB ( 1%) ggc
 callgraph optimization:   0.18 ( 1%) usr   0.12 ( 3%) sys   0.28 ( 2%) wall   11906 kB ( 2%) ggc
 varpool construction  :   0.04 ( 0%) …
Run Code Online (Sandbox Code Playgroud)

c++ gcc templates g++

8
推荐指数
2
解决办法
6276
查看次数

使用姐妹继承

让我们假设一些无法触及的(遗留)代码声明

struct B{
 public:
  void f(){}
};
Run Code Online (Sandbox Code Playgroud)

让我们假设有

struct A{
 public:
  virtual void f()=0;
};
Run Code Online (Sandbox Code Playgroud)

是否有可能使A子类调用B :: f而不显式调用f(),即代替

  struct C: public A, public B{
   void f(){
   B::f();
  }
 };
Run Code Online (Sandbox Code Playgroud)

有类似的东西

 struct C:virtual public A,virtual public B{

 };
Run Code Online (Sandbox Code Playgroud)

(注意,最后一个类是抽象的,因为没有定义编译器A :: f)

c++ inheritance multiple-inheritance virtual-inheritance

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

运算符优先级

可能重复:
cout <<它打印的函数调用顺序?
未定义的行为和序列点

为什么这段代码打印2 1 0?

#include <iostream>
struct A{
  int p;
  A():p(0){}
  int get(){
    return p++;
  }
};


int main(){
 A a;
 std::cout<<a.get()<<" "<<a.get()<<" "<<a.get()<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ operators

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

命名空间中的函数阴影

假设您有以下代码

namespace a{
  struct S{};
  //void f(int){}
}

namespace b{
  struct T{};
}


struct X{};

void f(X){}
void f(b::T){}
void f(a::S){}



namespace a{
  void g(){

    S s;b::T t; 
    X x;
    f(x);
    f(s);
    f(t);
  }

}

int main(){
  a::g();
}
Run Code Online (Sandbox Code Playgroud)

如果void f(int){}在名称空间中的(线3未被注释)中所定义,它阴影的后面的定义void f(b::T){}void f(a::S){},但不是void f(X){}.为什么?

c++ namespaces method-hiding

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