考虑以下程序:
struct ghost
{
// ghosts like to pretend that they don't exist
ghost* operator&() const volatile { return 0; }
};
int main()
{
ghost clyde;
ghost* clydes_address = &clyde; // darn; that's not clyde's address :'(
}
Run Code Online (Sandbox Code Playgroud)
我怎么得到clyde地址?
我正在寻找一种适用于所有类型对象的解决方案.C++ 03解决方案会很好,但我也对C++ 11解决方案感兴趣.如果可能,让我们避免任何特定于实现的行为.
我知道C++ 11的std::addressof函数模板,但我不想在这里使用它:我想了解标准库实现者如何实现这个函数模板.
考虑这个简单的例子:
template <class Type>
class smartref {
public:
smartref() : data(new Type) { }
operator Type&(){ return *data; }
private:
Type* data;
};
class person {
public:
void think() { std::cout << "I am thinking"; }
};
int main() {
smartref<person> p;
p.think(); // why does not the compiler try substituting Type&?
}
Run Code Online (Sandbox Code Playgroud)
转换运算符如何在C++中工作?(即)编译器何时尝试替换转换运算符后定义的类型?
我在下面的代码中重载了我的函数:
void function(char x, double y) {
cout << "char, double" << endl;
}
void function(int x, int y) {
cout << "int, int" << endl;
}
int main() {
function('a', 'b');
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,我说:"[警告] ISO C++说这些是模糊的,即使第一次转换的最差转换比第二次转换的最差转换"
编译器如何在此处进行隐式转换,以使哪个候选者是对的模糊不清?
请考虑以下代码:
struct A {
void operator++() const {}
};
void operator++(const A&) {}
int main () {
const A ca;
++ca; // g++ Error (as expected): ambiguous overload for ‘operator++’
A a;
++a; // g++ Warning: "ISO C++ says that these are ambiguous,
// even though the worst conversion for the first is better
// than the worst conversion for the second"
// candidate 1: void operator++(const A&)
// candidate 2: void A::operator++() const
}
Run Code Online (Sandbox Code Playgroud)
为什么g ++只发出警告而不是错误++a?换句话说,非成员函数如何比成员函数更合适? …
以下代码打印:
generic
overload
Run Code Online (Sandbox Code Playgroud)
但我想要的是在两种情况下都调用了重载或特化,而不是通用的.我不是试图将重载与模板专业化混合在一起,因为它们没有像我预期的那样工作.是否有任何模板魔法来实现这一目标?
#include <iostream>
class Interface {};
class Impl: public Interface {};
class Bar
{
public:
template<typename T> void foo(T& t) {
std::cout << "generic\n";
}
void foo(Interface& t) {
std::cout << "overload\n";
}
};
template<> void Bar::foo<Interface>(Interface& t) {
std::cout << "specialization\n";
}
int main() {
Bar bar;
Impl impl;
Interface& interface = impl;
bar.foo(impl);
bar.foo(interface);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 请原谅我,因为我对C++很新,但我在操作员歧义方面遇到了一些麻烦.对于我桌面上编译的代码,我认为它是特定于编译器的.但是,它无法在我的笔记本电脑上编译.我想我知道出了什么问题,但我看不到它的优雅方式.如果我犯了一个明显的错误,请告诉我.无论如何,这就是我要做的事情:
我创建了自己的Vector4类,它看起来像这样:
class Vector4
{
private:
GLfloat vector[4];
...
}
Run Code Online (Sandbox Code Playgroud)
然后我有这些运算符导致问题:
operator GLfloat* () { return vector; }
operator const GLfloat* () const { return vector; }
GLfloat& operator [] (const size_t i) { return vector[i]; }
const GLfloat& operator [] (const size_t i) const { return vector[i]; }
Run Code Online (Sandbox Code Playgroud)
我有转换运算符,以便我可以将我的Vector4类的实例传递给glVertex3fv,我有明显的原因下载.但是,涉及下载Vector4的调用对编译器来说是不明确的:
enum {x, y, z, w}
Vector4 v(1.0, 2.0, 3.0, 4.0);
glTranslatef(v[x], v[y], v[z]);
Run Code Online (Sandbox Code Playgroud)
以下是候选人:
candidate 1: const GLfloat& Vector4:: operator[](size_t) const
candidate 2: operator[](const GLfloat*, int) <built-in>
Run Code Online (Sandbox Code Playgroud)
当下载运算符已经在Vector4上定义时,为什么它会尝试将我的Vector4转换为GLfloat*?有没有一个简单的方法来解决这个问题?我只是犯了一个愚蠢的错误?在此先感谢您的帮助.
template <typename T>
class v3 {
private:
T _a[3];
public:
T & operator [] (unsigned int i) { return _a[i]; }
const T & operator [] (unsigned int i) const { return _a[i]; }
operator T * () { return _a; }
operator const T * () const { return _a; }
v3() {
_a[0] = 0; // works
_a[1] = 0;
_a[2] = 0;
}
v3(const v3<T> & v) {
_a[0] = v[0]; // Error 1 error C2666: 'v3<T>::operator …Run Code Online (Sandbox Code Playgroud) c++ ×7
ambiguity ×1
c++11 ×1
g++ ×1
inheritance ×1
opengl ×1
operators ×1
templates ×1
visual-c++ ×1