Why "throw std::string" is not correct?

C

Cheng Mo

Below code cannot be compiled.
try
{
throw std::string;
}
catch(...)
{
}

While below is correct.
try
{
throw std::string();
}
catch(...)
{
}

Why doesn't standard C++ parse "throw std::string" as "throw
std::string()"?
 
S

Sharad Kala

Cheng Mo said:
Below code cannot be compiled.
try
{
throw std::string;

std::string is name of a class.
}
catch(...)
{
}

While below is correct.
try
{
throw std::string();

std::string() creates an unnamed object.
}
catch(...)
{
}

It's legal to throw an object but not a class name.
Why doesn't standard C++ parse "throw std::string" as "throw
std::string()"?

They are different. That's the way language is designed, for more queries
should help you.

Sharad
 
D

Dietmar Kuehl

Cheng said:
Below code cannot be compiled.
throw std::string;

Throw takes an object as parameter, not a type.
'std::string' is a type, not an object.
While below is correct.
throw std::string();

'std::string()' is an object (of type 'std::string').
Why doesn't standard C++ parse "throw std::string" as "throw
std::string()"?

Why should it? There is a distinction between an object and
a type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top