1

When I declare a condition_variable, it may throw std::system_error.

But how about when I declare a POD type (e.g. int, double or float)?

Like the code below:

int main()
{
    //do something
    int i;    //will here throw exception?
}

If declaring a POD type may throw exception, how do I guarantee

void test() noexcept
{
    //do something
    int i;
}

is noexcept?

curiousguy
  • 8,038
  • 2
  • 40
  • 58
Caesar
  • 971
  • 6
  • 13
  • Are you asking is there any way to make throw an exception by declaring a variable? – AustinWBryan Jan 20 '16 at 05:49
  • yes. that is what I mean. – Caesar Jan 20 '16 at 05:50
  • 3
    Declaring a POD (plain old data: `int` etc) type won't throw an exception; however, any other constructor _may_ throw an exception. Check documentation. You can see that [`std::condition_variable`](http://en.cppreference.com/w/cpp/thread/condition_variable/condition_variable)'s constructor can throw an exception. – Tas Jan 20 '16 at 05:51
  • Why would you need this? What are you trying to achieve? – AustinWBryan Jan 20 '16 at 05:52
  • To @AustinWBryan, actually, I am checking whether my code is noexcept. – Caesar Jan 20 '16 at 05:54
  • @Caesar I [made an edit](http://stackoverflow.com/revisions/34892423/3) that I hope you find improves your question. The biggest thing I changed was _fundamental type_ which by that I assume you meant _POD type_ (plain old data). This should make the question more clear for others in the future. – Tas Jan 20 '16 at 05:58
  • @Tas Thank you. The fundamental type comes from here http://en.cppreference.com/w/cpp/language/types. – Caesar Jan 20 '16 at 06:00
  • I think you mean **definition** here, not declaration. A declaration like `extern int i` doesn't have any code associated with it. A definition like `int i = foo();` could throw an exception, but only from inside the ` foo()` part. – MSalters Jan 20 '16 at 09:12

3 Answers3

2

But how about declare a fundamental type (e.g., int, double or float)?

Declaring POD type objects won't cause an exception to be thrown.

Constructors of non-POD types can throw exceptions. Only the documents/source code of those types can help you figure out whether that will happen for a particular type.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

It is the constructor of std::condition_variable that can throw an exception. Primitive types like int and double do not have any constructors. They simply have some stack space allocated for them and that's it, plus a value being written if you initialize the variable. The only way this could cause an exception is if you overflow the stack and the ensuing undefined behaviour happens to throw one.

chris
  • 60,560
  • 13
  • 143
  • 205
0

POD types typically are initialized with initializer expressions instead of constructors. Like constructors, initializers can throw exceptions. But if you have neither a constructor nor an initializer, there's no code associated with the definition and therefore also no possibility for that code to throw.

MSalters
  • 173,980
  • 10
  • 155
  • 350