0

Can you say how can I assertion, is function noexcept(without passing arguments)? Thanks.

skypjack
  • 49,335
  • 19
  • 95
  • 187
ma13
  • 53
  • 1
  • 9
  • In general, you can't specify a function without also specifying arguments - there may be multiple overloads with the same name, some `noexcept` and others not. – Igor Tandetnik Dec 07 '16 at 14:59

2 Answers2

6

Assuming you are asking (thanks to @VittorioRomeo for the assumption):

How can I assert that a function is noexcept without calling it?

You can use the noexcept operator to do that, because its operands are unevaluated operands.
In other terms, you can do this:

void f(int) noexcept { }
void f(int, int) { }

int main() {
    static_assert(noexcept(f(0)), "!");
    static_assert(not noexcept(f(0, 0)), "!");
}

f won't be called in any case, that is the (let me say) nature of an unevaluated operand.

The most interesting part is that you can combine it with std::declval if you don't have a variable to be used as an argument and you don't know how to construct it.
As an example:

#include<utility>

struct S { S(int) {} };

void f(S) noexcept { }

int main() {
    static_assert(noexcept(f(std::declval<S>())), "!");
}

Note that I don't have to pass anything to construct S, even if it hasn't a default constructor.

This is usually enough to work around the requirement of passing no arguments.

Community
  • 1
  • 1
skypjack
  • 49,335
  • 19
  • 95
  • 187
0

Assuming you're asking:

How can I assert that a function is noexcept without calling it?

The answer is that you can't, because the noexcept specifier could be different between overloads. Example:

int a(int) noexcept;
int a(int, int);

// What would `noexcept(a)` mean?
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • #include #include void ff()noexcept { } class A { public: void f()noexcept { ff(); } }; void g(A* obj,void (A::*f)()) { static_assert(noexcept((obj->*f)),"kl"); } int main() { A a; g(&a,&A::f); } – ma13 Dec 07 '16 at 16:36
  • For example I think that this returned wrong value #include #include void ff()noexcept { } class A { public: void f()noexcept { ff(); } }; void g(A* obj,void (A::*f)()) { static_assert(noexcept((obj->*f)),"kl"); } int main() { A a; g(&a,&A::f); } Why it's asserted? – ma13 Dec 07 '16 at 16:37
  • Can you please post that on wandbox or something similar? – Vittorio Romeo Dec 07 '16 at 16:47
  • @MartinAyvazyan Note the the noexcept specifier is not part of the function type in C++11. That's why your example _works_ even if you would have expected the opposite. – skypjack Dec 07 '16 at 16:59
  • I have class( with as variadic template) and I need in other function check is this constexpr or no. What I should be done? – ma13 Dec 07 '16 at 17:28
  • I have class( with as variadic template) and I need in other function check is this constexpr or no. What I should be done? – ma13 Dec 07 '16 at 17:31
  • @MartinAyvazyan What are those types meant for? Are they member functions pointers or whatever? – skypjack Dec 07 '16 at 18:27
  • They're member functions – ma13 Dec 07 '16 at 20:21
  • If I want know is function, which's transferred as argument(pointer onto function), to have noexcept attribute or no, what I should be done? – ma13 Dec 08 '16 at 13:10