C++标准是否保证两个指向函数的指针始终相等?我理解这对于非内联函数通常是正确的.但是如果有一个内联函数并且在两个单独的编译单元中创建了指向该函数的指针,那么链接器是否会合并两个实例,或者它是否允许发出重复的函数?
如果上面的答案是"它们相等":如果有一个带内联函数的公共头文件,并且主程序和动态加载的插件(共享对象/ DLL)都创建了一个指向该函数的指针,它是否仍然成立?
在Windows 7,32位下的Visual Studio 2010上,unsigned long似乎是uint32_t和uint64_t的不同类型.请参阅以下测试程序:
#include <stdint.h>
#include <stdio.h>
template<class T, class U>
struct is_same_type
{
static const bool value = false;
};
template<class T>
struct is_same_type<T, T>
{
static const bool value = true;
};
#define TO_STRING(arg) TO_STRING_IMPL(arg)
#define TO_STRING_IMPL(arg) #arg
#define PRINT_SAME_TYPE(type1, type2) printf("%s (size=%d) %s %s (size=%d)\n", \
TO_STRING(type1), int(sizeof(type1)), \
is_same_type<type1, type2>::value ? "==" : "!=", \
TO_STRING(type2), int(sizeof(type2)))
int main(int /*argc*/, const char* /*argv*/[])
{
PRINT_SAME_TYPE(uint32_t, unsigned long);
PRINT_SAME_TYPE(uint64_t, unsigned long);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望它能打印出来 …
我的代码中有一个相对复杂的接口,并且希望将部分实现委托给另一个类,而不需要在实现中编写大量的转发函数.请参阅此简化示例代码:
#include <stdio.h>
struct Interface {
virtual void foo() = 0;
virtual void bar() = 0;
virtual ~Interface() {}
};
struct Delegate {
virtual void foo()
{ printf("foo\n"); }
};
struct Impl : public Interface, private Delegate {
// delegate foo to Delegate
using Delegate::foo;
void bar()
{ printf("bar\n"); }
};
int main() {
Interface* i = new Impl();
i->foo();
i->bar();
delete i;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在G ++抱怨foo没有在Impl中实现.然而,在Impl中有一个完美的函数foo,它只是从另一个父类中获取.为什么编译器不能正确填写vtable?
(注意:我知道在这个特定的例子中,Delegate可以从Interface派生.我想了解是否可以委派功能而无需从接口派生委托.)