Can you say how can I assertion, is function noexcept(without passing arguments)? Thanks.
2 Answers
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.
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?

- 90,666
- 33
- 258
- 416
-
#include
#include – ma13 Dec 07 '16 at 16:36void 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); } -
For example I think that this returned wrong value #include
#include – ma13 Dec 07 '16 at 16:37void 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? -
-
@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
-
-
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