0

I'm experiencing a weird behavior when using static_assert for asserting that the return type of a function object is the same as another type. Something like (This code is just for presenting the problem and not actually what i'm trying to do).

int foo() {
    return 0;
}

int main() {
    auto funcObj = std::bind(foo);
    static_assert(std::is_same<std::result_of<funcObj()>, int>::value, "FuncObj return type is not int");
    return 0;
}

The assertion fails. What's going wrong here?

Mr. Anderson
  • 1,609
  • 1
  • 13
  • 24
  • 3
    I guess you meant: `static_assert(std::is_same::value, "FuncObj return type is not int");` – Piotr Skotnicki Jun 18 '15 at 14:05
  • Please post actual code that actually generates the error message in question. When something happens that you don't understand, simplifying is quite likely to delete the part that makes it happen if you do not **test** that the simplification did not delete the part that makes it happen. – Yakk - Adam Nevraumont Jun 18 '15 at 14:14

2 Answers2

3

std::result_of<?> is a useless type to use directly.

typename std::result_of<?>::type and std::result_of_t<?> (in C++14) what you want.

An easy way to get a good error message for this kind of thing is:

std::result_of<decltype(funcObj)()> x = 3;

which should generate error messages that make it clear that the lhs type is not int like you expected. (cannot convert int to 'some complex type' usually).

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
1

The line

static_assert(std::is_same<std::result_of<funcObj()>, int>::value, "FuncObj return type is not int");

needs to be

static_assert(std::is_same<typename std::result_of<decltype(funcObj)()>::type, int>::value, "FuncObj return type is not int");
        // Missing pieces  ^^^^^^^^                                 ^^ ^^^^^^
R Sahu
  • 204,454
  • 14
  • 159
  • 270