Today, I saw a post on the BBS VC version, saying that VC has bugs when compiling templates, his test code is as follows:
// templatebug.cpp: Define the entry point of the console application.
//
#include "stdafx.h"
// Define an interface first
Template
Class A
{
PUBLIC:
Virtual Int Somefunc (const Dattype & Value) = 0;
}
// b To achieve this interface, it is actually compiled!
Template
Class B: Public a
{
PUBLIC:
Virtual Int Somefunc (const char * & value) {return 0;};
}
/ / Change to this to compile
Template
Class C: Public a
{
PUBLIC:
Virtual Int Somefunc (Char * const & value) {return 0;};
}
// const char * and char * constant meaning are very different, but the parameterization process of the template is
// After confusing two, should this be a bug?
INT _Tmain (int Argc, _tchar * argv [])
{
B
C
Return 0;
}
One of the comments is very interesting, but this is not a bug. On the contrary, it should be said that it is in line with C specification,
It is not a compiler to confuse both, but our author confuses both.
Let's see two references for Const Char * & P and Char * Const & P:
Both are references to an object.
But the former's "this object" is const char *, a pointer to const char, pay attention! although
However, this pointer points to CHAR can't change, but the value of this pointer itself can be changed, that is,
He can be changed to another Const char object.
The latter "This object" is char *, a pointer to char. Things point to this pointer is
Changed, but this pointer itself cannot change, you can't let him point to another char object.
If you feel that this expression is not clear enough, please compile the following code:
Void fconst (const char * & p) {
* p = 'l'; // error C2166: l Specifies constant object
P ; // ok!
}
Void Constf (Char * const & p) {
* p = 'l'; // ok!
P ; // error C2166: 1 Specify constant objects
}
Int main () {
Char str [] = "Hello World";
Char * p = & str [2];
Fconst (p);
Constf (p);
}
Now Const Char * & Char * Const & the difference is already clear, then we look at how the template is specialized by the author's SomeFunc parameters is a DataType's const reference, so-called Const Reference,
It is said that you can't change the status of the object referenced by this Reference, so Const DataType & and
DataType const & is equivalent, that is, Template a
The parameter is a common reference to an object. The status of the object referenced by this reference is not in the functional domain.
Can be changed.
So very clear, cHAR *> is specialized as the base class of Class B and C, then its pure virtual member function
The parameters of someFunc are specialized to be a const reference for char *, and somefunc has a form.
Int Somefunc (Char * Const &), so if defined as int Somefunc (const char * & value)
Will not match the pure virtual function, the normal virtual function turning nature, and the pure virtual function is not defined:
Error C2259: "B
WITH
[
Something1 = Int,
Something2 = Int
]
Due to the following members:
"INT A
WITH
[
DataType = char *
]
Tempbug.cpp (5): See "A
WITH
[
DataType = char *
]
The so-called BUG will not attack from the break. Ha ha.