模板函数和常量/非常量参考参数
我是 C++ 新手,从书本上学习,所以我的推理可能相当迂腐或狭隘。
在模板函数的情况下,我读到当参数通过引用传递时,只允许从引用/指针到非常量到引用/指针到常量的转换。
这意味着我认为
template <typename T> int compare(T&, T&);
在调用 Compare(ci1, ci1) 时应该失败,因为 ci1 是 consntant int,因为引用参数不允许从 Const 到 NonCost 的转换。
然而它在我的编译器(Visual C++ 10)中工作。有人可以解释一下我做错了什么吗?
template <typename T> int compare(T&, T&);
template <typename T> int compare(T &v1, T &v2)
{
// as before
cout << "compare(T, T)" << endl;
if (v1 < v2) return -1;
if (v2 < v1) return 1;
return 0;
}
const int ci1 = 10;
const int ci2 = 20;
int i1 = 10;
int i2 = 20;
compare(ci1, ci1);
compare(i1, i1);
New to C++ and learning from books so I can be quite pedantic or miopic in my reasoning.
In the case of template functions, I have read that when a parameter is passed by Reference, only conversions from Reference / Pointer to NonConst to Reference / Pointer to Const are alllowed.
That means I believe that
template <typename T> int compare(T&, T&);
should fail when calling compare(ci1, ci1), with ci1 being consntant int, as conversions from Const to NonCost are not allowed for Reference parameters.
However it works in my compiler (Visual C++ 10). Can someone explain me what I get wrong?
template <typename T> int compare(T&, T&);
template <typename T> int compare(T &v1, T &v2)
{
// as before
cout << "compare(T, T)" << endl;
if (v1 < v2) return -1;
if (v2 < v1) return 1;
return 0;
}
const int ci1 = 10;
const int ci2 = 20;
int i1 = 10;
int i2 = 20;
compare(ci1, ci1);
compare(i1, i1);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该调用
产生类型为 const int 的 T(以您喜欢的表示法)。
有效的函数签名是
您可以使用 typeid(x).name() 来检查您实际拥有的类型。
注意:使用 g++ 会产生一些不可理解的简短形式,然后您需要使用特殊的 g++ 特定的运行时 lib 函数来解码。
干杯&嗯。
The call
yields T as type
const int
(in your preferred notation).The effective function signature is then
You can use
typeid(x).name()
the check out what types you actually have.Note: with g++ that yields some ungrokkable short forms, which you then need to use a special g++-specific runtime lib function to decode.
Cheers & hth.
T
将是变量的任何类型 - 在您的情况下const int
,因此compare
的最终实例化将类似于您的第一种情况
compare(ci1,ci2)
与compare(i1,i2)
类似。
T
will be whatever type the variable is - in your caseconst int
, so the final instantiation ofcompare
will look likein your first case with
compare(ci1,ci2)
and likewith
compare(i1,i2)
.在第一种情况下,模板是用
T = const int
实例化的,这很好。如果您尝试比较(i1,ci1),您将收到错误,因为它将无法找到与 int & 和 const int 兼容的模板参数&。将签名更改为
compare(const T &, const T &)
将解决该问题。In the first case, the template is instantiated with
T = const int
, which is fine.You will get an error if you try
compare(i1, ci1)
, since that will fail to find a template argument compatible with bothint &
andconst int &
. Changing the signature tocompare(const T &, const T &)
will fix that problem.在比较(ci1,ci1)的情况下; T 将是 const int。这就是它起作用的原因
In the case of compare(ci1, ci1); T will be const int. This is why it works
这是可以接受的,因为当您用
int const
替换T
时,可以实例化该函数。This is acceptable because the function can be instantiated when you substitute
int const
forT
.