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) 如果被调用的函数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
,例如,不会抛出)
如果我做
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 .C
A
[代码源自`is_base_of`是如何工作的?]
想象一下你有一个文件啊
#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?
假设你有
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) 我有一个大量使用模板的项目.最近编译时间突然上升.我想知道是否有办法看到哪些类/行需要最多的时间来编译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) 让我们假设一些无法触及的(遗留)代码声明
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)
可能重复:
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) 假设您有以下代码
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++ ×9
templates ×5
g++ ×2
inheritance ×2
cancellation ×1
exception ×1
gcc ×1
linker ×1
namespaces ×1
operators ×1
pthreads ×1