C ++ - 引用类型的模板实例化 [英] C++ - template instantiation with reference type
问题描述
我听过一些关于参考文献问题和此解决方案的问题。我对C ++委员会术语不是很好,但我理解链接中的移至DR注释意味着这是符合标准的编译器应遵循的当前解释。
I've heard a little bit about reference-to-reference problem and this resolution. I'm not very good with C++ Committee terminology, but I understand the "Moved to DR" annotation in the link means that this is the current interpretation that standard-conforming compilers should adhere to.
我有这个示例代码,我不明白:
I have this sample code that I can't understand:
template <typename T>
struct C {
void f(T&) { }
void f(const T&) { }
};
int main() {
C<int> x; // OK
C<int&> y; // compile error: f cannot be overloaded
C<const int&> z; // compile error: f cannot be overloaded
}
c $ c> C C< int&>
case:不应该生成与 C< int> / code>(至少根据Stroustrup的解决方案)?
I understand the error in C<const int&>
case: using rules from DR #106 we get two methods with the same signature f(const int&). What I don't get is the C<int&>
case: shouldn't it generate exactly the same code as C<int>
(at least according to Stroustrup's resolution)?
推荐答案
DR仅表示缺陷报告我的知识,描述的分辨率还没有(还)达到标准。因此,我认为严格遵守C ++ 03实现不应该编译这段代码,因为它正在形成一个引用的引用。
DR only means "Defect Report", and to my knowledge, the described resolution hasn't made it (yet) to the standard. For this reason, I believe a strictly conforming C++03 implementation should not compile this code because of it is forming a reference to a reference.
关于此问题的不错的答案。
这篇关于C ++ - 引用类型的模板实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!