0

I am currently writing a public library for vector and matrix operations. My library, as it stands now, only accepts arithmetic types and complex numbers (by using std::complex<T>). Since I value-initialize an array of type T, where T might be std::complex<T>, and since std::complex<T>'s default constructor is not noexcept, I'm wondering whether it is the case to have my default constructor have a conditional noexcept for this single case, so that the constructor is noexcept only when T's default constructor is marked noexcept. I don't know if that makes any sense; currently, I can't find a reason as to why this might be helpful. But on the other hand, I am completely unaware of what anyone using the library might do with it (they might, perhaps, store multiple Math::Vectors inside a std::vector too, I guess?).

So long story short: I'm wondering whether something like this would make sense, to cover the case where std::complex<T> is passed:


private:
    T math_vector[Size]{}; // T might be std::complex
public:
    constexpr Vector() noexcept(std::is_nothrow_default_constructible_v<T>) = default;
  • 1
    @PepijnKramer Um, most constructors are noexcept. You must have a noexcept move constructor if you want `std::vector` to move your elements when growing. If your class only has an `int` member it is also not going to throw an exception and marking it as noexcept is perfectly fine. – NathanOliver Jul 18 '22 at 12:04
  • IIRC I have seen STL containers do this. I should have to recheck though so take this with a grain of salt. – user15532034 Jul 18 '22 at 12:06
  • @NathanOliver Ok I stand corrected. (after checkeing : https://en.cppreference.com/w/cpp/language/noexcept_spec) – Pepijn Kramer Jul 18 '22 at 12:07
  • But still, I am very unsure whether I'm doing this correctly or if it's needed for std::complex type. Also, I noticed that I have a default value initialization, [[[[ and I forgot whether that takes the precedence over default constructors. Maybe that makes the conditional noexcept useless since it might still throw.]]]]] EDIT: nevermind. The constructor takes the precedence. So is the approach OK? – user15532034 Jul 18 '22 at 12:09
  • `std::complex` doesn't do dynamic allocation so it's not really needed. – NathanOliver Jul 18 '22 at 12:11
  • I see. Should I leave it in case I change the allowed types in the future, or is that considered a bad approach? Thanks for the quick answer, by the way. – user15532034 Jul 18 '22 at 12:16
  • 1
    Had to double check t make sure but looks like the default constructor will be implicitly noexcept as long as the members of the class have noexcept default constructors: https://stackoverflow.com/questions/36161188/is-a-defaulted-constructor-assignment-noexcept-constexpr-by-default – NathanOliver Jul 18 '22 at 12:22
  • Perfect, that makes some sense I guess. I too completely forgot about the default keyword there. – user15532034 Jul 18 '22 at 12:26
  • 2
    See the section starting _"...functions declared without noexcept specifier except for..."_ https://en.cppreference.com/w/cpp/language/noexcept_spec which includes _"...default constructors, copy constructors, move constructors that are implicitly-declared or defaulted on their first declaration unless..."_ – Richard Critten Jul 18 '22 at 12:49

0 Answers0