For Programmers: Free Programming Magazines  


Home > Archive > VC Language > June 2005 > Can't throw templated class









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Can't throw templated class
erik.wright@radialpoint.com

2005-06-09, 8:59 pm

Hello,

I am having a strange problem in VC++ in Visual Studio .NET 2003. In
essence, I am unable to throw a reference to a templated class in
certain situations.

The compiler output is as follows (with /W4 - warning level 4 -
enabled):

------ Build started: Project: testproj, Configuration: Debug Win32
------

Compiling...
testproj.cpp
c:\testproj\testproj\testproj.cpp(6) : error C2700: 'const
TemplatedException<T>' : cannot be thrown (use /W4 for more info)
with
[
T=int
]
c:\testproj\testproj\testproj.cpp(6) : warning C4671:
'TemplatedException<int>' : the copy constructor is inaccessible

Build log was saved at "file://c:\testproj\testproj\Debug\BuildLog.htm"
testproj - 1 error(s), 1 warning(s)


---------------------- Done ----------------------

Build: 0 succeeded, 1 failed, 0 skipped

The code that causes this is below. Interestingly, if I make the
exception type untemplated, everything compiles! Any ideas?

/*********/
/* FAILS */
/*********/

template <typename stringT> class TemplatedException
{
};
void PleaseThrow( const TemplatedException<int>& ex )
{
throw ex;
}
int main(int argc, char* argv[])
{
PleaseThrow(TemplatedException<int>());
}


/************/
/* SUCCEEDS */
/************/
class NonTemplatedException
{
};
void PleaseThrow( const NonTemplatedException& ex )
{
throw ex;
}
int main(int argc, char* argv[])
{
PleaseThrow(NonTemplatedException());
}

Gene Bushuyev

2005-06-09, 8:59 pm

Specifying /W4 you get more information why the compiler is failing:

warning C4671: 'TemplatedException<int>' : the copy constructor is
inaccessible

Looks like a bug in VC7.1. VC8-B2 compiles without problem.

<erik.wright@radialpoint.com> wrote in message
news:1118350957.399027.37830@g44g2000cwa.googlegroups.com...
> Hello,
>
> I am having a strange problem in VC++ in Visual Studio .NET 2003. In
> essence, I am unable to throw a reference to a templated class in
> certain situations.
>
> The compiler output is as follows (with /W4 - warning level 4 -
> enabled):



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com