Re: Template member function cast bug in VC80SP1 (and more...)

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



// No, it is not an omission. Using-declaration brings in a
// name only for the purpose of overload resolution. The type
// of the name remains the same.
Thanks for the quote.




// Why do you think it's illegal? It compiles both by VC++2005
// and Comeau online compiler wihtout any warning.

No, VC2005 SP1 does NOT compile:

template <typename S>
class has_something_named_swap
{
class yes { char dummy[2]; };
typedef char no;
template <typename T>
static yes test(T*, size_t = sizeof(&T::swap));
static no test(void*, size_t = 0);
public:
static const bool value = sizeof(test( (S*)0 ))!= sizeof(no);
};
int main(int argc, char* argv[])
{
bool b1 = has_something_named_swap<double>::value;
bool b2 = has_something_named_swap<int>::value;
return 0;
}

1>------ Build started: Project: test03, Configuration: Release Win32
------
1>Compiling...
1>test03.cpp
1>.\test03.cpp(20) : error C2825: 'T': must be a class or namespace when
followed by '::'
1> .\test03.cpp(35) : see reference to class template
instantiation 'has_something_named_swap<S>' being compiled
1> with
1> [
1> S=double
1> ]
1>.\test03.cpp(20) : error C2039: 'swap' : is not a member of '`global
namespace''
1>.\test03.cpp(20) : error C2275: 'T' : illegal use of this type as an
expression
1> .\test03.cpp(26) : see declaration of 'T'
1>.\test03.cpp(20) : error C2275: 'T' : illegal use of this type as an
expression
1> .\test03.cpp(26) : see declaration of 'T'
1>.\test03.cpp(20) : error C2146: syntax error : missing ')' before
identifier 'swap'
1>.\test03.cpp(20) : error C2825: 'T': must be a class or namespace when
followed by '::'
1> .\test03.cpp(36) : see reference to class template
instantiation 'has_something_named_swap<S>' being compiled
1> with
1> [
1> S=int
1> ]
1>.\test03.cpp(20) : error C2039: 'swap' : is not a member of '`global
namespace''
1>.\test03.cpp(20) : error C2275: 'T' : illegal use of this type as an
expression
1> .\test03.cpp(26) : see declaration of 'T'
1>.\test03.cpp(20) : error C2275: 'T' : illegal use of this type as an
expression
1> .\test03.cpp(26) : see declaration of 'T'
1>.\test03.cpp(20) : error C2146: syntax error : missing ')' before
identifier 'swap'
1>Build log was saved at
"file://c:\Source\test03\test03\Release\BuildLog.htm"
1>test03 - 10 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========






// There was a topic about the same problem a couple of days
// ago:
// "Some issues with using sizeof() in template definitions"
// http://groups.google.com/group/microsoft.public.vc.language/msg/
// 4c1dac7ab6ee73ef
// I think it's a bug in VC++2005.

Thanks for the reference, I know it's a ICE in VC2003, but I find it
bizarre that the bug fix is "emit a graceful compiler error" instead of
"compile it right" :)




// Alternatively, you can use `__if_exists' compiler intrinsic:

If I understand correctly, this is not a compile-time constant




// // (problem 3: very important)
// [Did you mean (c.*memptr)(...), where `memptr' is third
// parameter?]

NO! this is the key point: I don't want to USE the runtime pointer, but
a compile-time function call (in fact I pass a pointer just to select
the right overload)




// The error is misleading. The real problem is again
// inaccessibility of `VECTOR::erase' due to private
// inheritance. Comeau online reports correct error about it.
// Once appropriate `MAP_BASE::erase' is available both Comeau
// and VC++ compile the code without any problem.

Are you sure? All compilers I tried do compile with both public/private
inheritance, including Comeau (I copied and pasted the sample I posted
previously).
VC8 SP1 instead never compiles. Which settings did you use?



1>------ Build started: Project: test03, Configuration: Release Win32
------
1>Compiling...
1>test03.cpp
1>.\test03.cpp(81) : error C2664: 'erase_gap2' : cannot convert
parameter 3 from 'overloaded-function' to
'std::_Vector_iterator<_Ty,_Alloc> (__thiscall VECTOR<T>::*
)(iterator_t)'
1> with
1> [
1> _Ty=std::pair<double,double>,
1> _Alloc=std::allocator<std::pair<double,double>>,
1> T=std::pair<double,double>,
1>
iterator_t=std::_Vector_iterator<std::pair<double,double>,std::allocator
<std::pair<double,double>>>
1> ]
1> None of the functions with this name in scope match the target
type
1> .\test03.cpp(94) : see reference to function template
instantiation 'std::_Vector_iterator<_Ty,_Alloc>
erase_gap<MAP<key_t,mapped_t>>(container_t
&,std::_Vector_iterator<_Ty,_Alloc>)' being compiled
1> with
1> [
1> _Ty=std::pair<double,double>,
1> _Alloc=std::allocator<std::pair<double,double>>,
1> key_t=double,
1> mapped_t=double,
1> container_t=MAP<double,double>
1> ]
1>Build log was saved at
"file://c:\Source\test03\test03\Release\BuildLog.htm"
1>test03 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
==========


--
Every sequence of problems that converges to a solution
Contains a sequence of solutions that converges to a new problem.
-- Mycroft Holmes


.



Relevant Pages

  • Re: Template classes with virtual member functions
    ... That doesn't compile. ... Referrer::OnReferenceResolved() takes Reference *, but Reference is a template. ... template <class ReferrerType> ...
    (microsoft.public.vc.language)
  • Re: [!! RANT !!] This is a hack at best (C4251)
    ... Micro$oft support for STL and standards in general is absolutely appaling. ... The built DLL exports the symbols, so I am not sure why the compiler issues the warnings. ... The function template is completely ignored and not even exported. ... I hope you can provide a fix as to how to get this to work (i.e. compile ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Make t2 const: T1 (&aPlus(T1(&t1)[S],T2(&t2)[S]))[S]
    ... >>I'm not sure exactly what you're trying to do, however you can overload ... > uncomment the following line and it won't compile ... Also, you don't need to specify the template parameters, they can be ... void test_aPlus ...
    (comp.lang.cpp)
  • Re: Avoiding C++ Templates In Cipher Implementation
    ... >> asymmetric ciphers implemented as templates, and they compile without ... sense to have it as a template parameter. ... template instantiation is only a ...
    (sci.crypt)
  • Re: Inheritance & Template parameters
    ... > I would like to apply inheritance to a template parameter, ... How is this going to compile? ... What's so bad if it's a functor of the base class and not ...
    (comp.lang.cpp)