所以我遇到的问题很复杂,但很重要。我不知道这是C++的特性还是编译器的特性。但是正在发生的是,来自不同类的两个不同的函数得到相同的地址:
//-- My Code --//
class A {
public:
int myFunc() {
return 3;
}
};
//-- Other Code --//
namespace glm{
namespace detail
{
template <typename T>
GLM_FUNC_QUALIFIER typename tvec3<T>::size_type tvec3<T>::length() const
{
return 3;
}
//... Omitted
}
}
//-- Test --//
TypelessMember::Func<int> funcOverridenA = TypelessMember::func(&A::myFunc); // The original function pointer (pretending to be a member function of a "typeless" class)
auto equivalenceFunc = &glm::detail::tvec3<unsigned char>::length; // Get the false equivalence member function pointer
auto equivalence = *(TypelessMember::Func<int>*)&equivalenceFunc; // Pretend that this is a different type of function
if (equivalence == funcOverridenA) {
puts("False function equivalence"); // This block of code is hit
return false;
}
//-- Other test --//
A valueA = {};
TypelessMember* typelessA = typeless(&valueA);
int v = (typelessA->*funcOverridenA)(); // This calls the glm function (I guess because it was produced in the PCH first)显然,我不希望A::myFunc的函数指针等价于glm长度函数的函数指针。那我该怎么办?这是否可以避免呢?我主要关心的是,我希望能够有两个独特的函数指针。
发布于 2015-08-20 19:21:27
这是MS编译器中已知的问题。当他们删除函数的"unneded“副本时,比如实例化的模板,他们会查看函数包含的内容,而不是它们的名称。
您有两个函数,不接受参数并返回值3,它们当然会生成完全相同的代码。链接器只保留一份拷贝,相信没有人想要一些代码的相同副本。
https://stackoverflow.com/questions/32126133
复制相似问题