M
michael.young
I have a situation where I need to specialize a class template that
has two template parameters. Unfortunately, it needs to be
specialized for multiple cases, and, in one case, it leads to a
"conflict" - i.e., the specialization to be used is ambiguous. No
problem - since the one case specializes for the first parameter and
the other case specializes the second, I figured I could just provide
a third specialization for both parameters that covers that particular
case. Unfortunately, I'm getting errors to the effect that this
explicit specialization is not allowed. (I used the Borland C++
Builder 2006 compiler, but then I checked it using the online Comeau C+
+ compiler, which also yielded a similar error.) Can anyone explain
what I'm doing wrong? Any help would be greatly appreciated.
The code is pasted at the bottom of this message, after the diagnostic
message from the Comeau compiler.
Thanks,
Michael
Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for ONLINE_EVALUATION_BETA1
Copyright 1988-2007 Comeau Computing. All rights reserved.
MODE:strict errors C++ noC++0x_extensions
"ComeauTest.c", line 79: error: "const TestPOD<int, char>
&TestPOD<int,
char>:perator=(const int &)" is not an entity that can be
explicitly specialized
TestPOD< int, char >:perator= ( const int& source_value )
^
1 error detected in the compilation of "ComeauTest.c".
In strict mode, with -tused, Compile failed
//********* CODE STARTS HERE *************
#include <iostream>
template < typename T1, typename T2 >
struct TestPOD
{
const TestPOD< T1, T2 >&
operator= ( const T1& source_value ) ;
T1 value_ ;
} ;
template < typename T1, typename T2 >
const TestPOD< T1, T2 >&
TestPOD< T1, T2 >:perator= ( const T1& source_value )
{
std::cout << "In non-specialized version..." << std::endl ;
value_ = source_value ;
std::cout << "value = " << value_ << std::endl ;
return *this ;
}
//
******************************************************************************
// Specialized : T1 = int
template < typename T2 >
struct TestPOD< int, T2 >
{
const TestPOD< int, T2 >&
operator= ( const int& source_value ) ;
int value_ ;
} ;
template < typename T2 >
const TestPOD< int, T2 >&
TestPOD< int, T2 >:perator= ( const int& source_value )
{
std::cout << "In specialized version (T1 = int)..." << std::endl ;
value_ = source_value ;
std::cout << "value = " << value_ << std::endl ;
return *this ;
}
//
******************************************************************************
// Specialized : T2 = char
template < typename T1 >
struct TestPOD< T1, char >
{
const TestPOD< T1, char >&
operator= ( const T1& source_value ) ;
T1 value_ ;
} ;
template < typename T1 >
const TestPOD< T1, char >&
TestPOD< T1, char >:perator= ( const T1& source_value )
{
std::cout << "In specialized version (T2 = char)..." << std::endl ;
value_ = source_value ;
std::cout << "value = " << value_ << std::endl ;
return *this ;
}
//
******************************************************************************
// Specialized : T1 = int, T2 = char
template < >
struct TestPOD< int, char >
{
const TestPOD< int, char >&
operator= ( const int& source_value ) ;
int value_ ;
} ;
template < >
const TestPOD< int, char >&
TestPOD< int, char >:perator= ( const int& source_value )
{
std::cout << "In specialized version (T1 = int, T2 = char)..." <<
std::endl ;
value_ = source_value ;
std::cout << "value = " << value_ << std::endl ;
return *this ;
}
//
******************************************************************************
//TestPOD< int, long > g_test_instance ; // Works
//TestPOD< long, char > g_test_instance ; // Works
TestPOD< int, char > g_test_instance ;
// 1) E2295 Too many candidate template specializations from
'TestPOD<T1,T2>'
// 2) Define specialization - TestPOD< int, char > - uncomment lines
65-87
// Borland C++ Builder 2006 now complains...
// line 80 - E2473 Invalid explicit specialization of
'TestPOD<int,char>:perator=(const int&)'
int
main( int argc, char* argv[] )
{
g_test_instance = 3 ;
return 0 ;
}
has two template parameters. Unfortunately, it needs to be
specialized for multiple cases, and, in one case, it leads to a
"conflict" - i.e., the specialization to be used is ambiguous. No
problem - since the one case specializes for the first parameter and
the other case specializes the second, I figured I could just provide
a third specialization for both parameters that covers that particular
case. Unfortunately, I'm getting errors to the effect that this
explicit specialization is not allowed. (I used the Borland C++
Builder 2006 compiler, but then I checked it using the online Comeau C+
+ compiler, which also yielded a similar error.) Can anyone explain
what I'm doing wrong? Any help would be greatly appreciated.
The code is pasted at the bottom of this message, after the diagnostic
message from the Comeau compiler.
Thanks,
Michael
Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for ONLINE_EVALUATION_BETA1
Copyright 1988-2007 Comeau Computing. All rights reserved.
MODE:strict errors C++ noC++0x_extensions
"ComeauTest.c", line 79: error: "const TestPOD<int, char>
&TestPOD<int,
char>:perator=(const int &)" is not an entity that can be
explicitly specialized
TestPOD< int, char >:perator= ( const int& source_value )
^
1 error detected in the compilation of "ComeauTest.c".
In strict mode, with -tused, Compile failed
//********* CODE STARTS HERE *************
#include <iostream>
template < typename T1, typename T2 >
struct TestPOD
{
const TestPOD< T1, T2 >&
operator= ( const T1& source_value ) ;
T1 value_ ;
} ;
template < typename T1, typename T2 >
const TestPOD< T1, T2 >&
TestPOD< T1, T2 >:perator= ( const T1& source_value )
{
std::cout << "In non-specialized version..." << std::endl ;
value_ = source_value ;
std::cout << "value = " << value_ << std::endl ;
return *this ;
}
//
******************************************************************************
// Specialized : T1 = int
template < typename T2 >
struct TestPOD< int, T2 >
{
const TestPOD< int, T2 >&
operator= ( const int& source_value ) ;
int value_ ;
} ;
template < typename T2 >
const TestPOD< int, T2 >&
TestPOD< int, T2 >:perator= ( const int& source_value )
{
std::cout << "In specialized version (T1 = int)..." << std::endl ;
value_ = source_value ;
std::cout << "value = " << value_ << std::endl ;
return *this ;
}
//
******************************************************************************
// Specialized : T2 = char
template < typename T1 >
struct TestPOD< T1, char >
{
const TestPOD< T1, char >&
operator= ( const T1& source_value ) ;
T1 value_ ;
} ;
template < typename T1 >
const TestPOD< T1, char >&
TestPOD< T1, char >:perator= ( const T1& source_value )
{
std::cout << "In specialized version (T2 = char)..." << std::endl ;
value_ = source_value ;
std::cout << "value = " << value_ << std::endl ;
return *this ;
}
//
******************************************************************************
// Specialized : T1 = int, T2 = char
template < >
struct TestPOD< int, char >
{
const TestPOD< int, char >&
operator= ( const int& source_value ) ;
int value_ ;
} ;
template < >
const TestPOD< int, char >&
TestPOD< int, char >:perator= ( const int& source_value )
{
std::cout << "In specialized version (T1 = int, T2 = char)..." <<
std::endl ;
value_ = source_value ;
std::cout << "value = " << value_ << std::endl ;
return *this ;
}
//
******************************************************************************
//TestPOD< int, long > g_test_instance ; // Works
//TestPOD< long, char > g_test_instance ; // Works
TestPOD< int, char > g_test_instance ;
// 1) E2295 Too many candidate template specializations from
'TestPOD<T1,T2>'
// 2) Define specialization - TestPOD< int, char > - uncomment lines
65-87
// Borland C++ Builder 2006 now complains...
// line 80 - E2473 Invalid explicit specialization of
'TestPOD<int,char>:perator=(const int&)'
int
main( int argc, char* argv[] )
{
g_test_instance = 3 ;
return 0 ;
}