chetzacoalt
chetzacoalt

Reputation: 157

c++ std::function type cheking correct?

I wish to use std::function type from so that function signature is checked on assignment. But I fail to understand what is happening in this case

//g++  5.4.0
#include <iostream>
#include <functional>
int f(int x)      { std::cout << "//f:int->int\n";    return x; }
int g(double x)   { std::cout << "//g:double->int\n"; return 1; }
int main()
{
    std::function<int(int)> fct;
    fct = f; fct(1);
    fct = g; fct(1);
}
//trace
//
//f:int->int
//g:double->int

The behavior for f is what I want, but I thought that "fct=g;" would cause a compile time error.

Any light on this case please ?

Upvotes: 3

Views: 82

Answers (3)

chetzacoalt
chetzacoalt

Reputation: 157

to complete the answer from @gaurav, complete scenarii using implicit conversion (it did surprise me, so I add it for anyone interested)

//g++  5.4.0
#include <iostream>
#include <functional>

class C{ 
    public : 
    int i; 
    C(int _i) : i(_i)  { std::cout << "//C::C(int " << i << ")\n"; } 
};
class D{ 
    public : 
    int i; 
    D(int _i) : i(_i)  { std::cout << "//D::D(int " << i << ")\n"; }
    D(C c)    : i(c.i) { std::cout << "//implicit conversion D::D(C " << c.i << ")\n"; }
};

int f(C c)   { std::cout << "//f:C->int : "; return c.i; }
int g(D d)   { std::cout << "//g:D->int : "; return d.i; }

int main()
{
    {
        std::cout << "//--- test implicit conversion\n";
        C c(1); 
        D d(2);
        d=c;
    }
    {
        std::function<int(C)> fct; C c(1); D d(2);
        std::cout << "//direct calls\n";
        std::cout <<   f(c) << "\n";
        // std::cout << "//" <<   f(d) << "\n";  // no conversion D->C provided by class C -->> error: could not convert ‘d’ from ‘D’ to ‘C’
        std::cout <<   g(d) << "\n";         
        std::cout <<   g(c) << "\n";             // implicit conversion, then g:D->int
    }
    {
        std::cout << "//case function:C->int\n";
        std::function<int(C)> fct; C c(1); D d(2);
        fct = f; std::cout << "//" << fct(c) << "\n";
        //std::cout << "//" << fct(d) << "\n";   // no conversion D->C provided by class C -->> error: could not convert ‘d’ from ‘D’ to ‘C’
        fct = g; std::cout << "//" << fct(c) << "\n";
        //std::cout << "//" << fct(d) << "\n";   // no conversion D->C provided by class C -->> no match for call to ‘(std::function<int(C)>) (D&)’
    }
    {
        std::cout << "//appels via function : D -> int\n";
        std::function<int(D)> fct; C c(1); D d(2);
        //fct = f;    // conversion D->C would be meaningless to f
                      // -->> error: no match for ‘operator=’ (operand types are ‘std::function<int(D)>’ and ‘int(C)’)
        fct = g; std::cout << "//" << fct(d) << "\n";
        std::cout << "//" << fct(c) << "\n";      // implicit conversion, then g:D->int
    }
}

//trace
//
//--- test implicit conversion
//C::C(int 1)
//D::D(int 2)
//implicit conversion D::D(C 1)
//C::C(int 1)
//D::D(int 2)
//direct calls
//f:C->int : 1
//g:D->int : 2
//implicit conversion D::D(C 1)
//g:D->int : 1
//case function:C->int
//C::C(int 1)
//D::D(int 2)
//f:C->int : //1
//implicit conversion D::D(C 1)
//g:D->int : //1
//appels via function : D -> int
//C::C(int 1)
//D::D(int 2)
//g:D->int : //2
//implicit conversion D::D(C 1)
//g:D->int : //1

Upvotes: 0

bolov
bolov

Reputation: 75688

std::function accepts any callable that where the parameters can be converted and the return type is convertible. If they aren't you get a compilation error. For instance:

int h(double& x);

std::function<int(int)> fct;
fct = h; // <- compiler error

Upvotes: 2

Gaurav Dhiman
Gaurav Dhiman

Reputation: 1173

std::function is flexible and uses type erasure underneath, so if you have std::function object with with signature std::function<R(Args...)>, it will accept any Callable that can be called with type Args... and returns type R,

So in your case for std::function<int(int)>, function of type int g(double); can be called with type int arguments compiler will just promote int to double,

If you run this code

#include <iostream>
#include <functional>
int f(int x)      { std::cout << x << " " << "//f:int->int\n";    return x; }
int g(double x)   { std::cout << x << " " << "//g:double->int\n"; return 1; }
int main()
{
    std::function<int(int)> fct;
    fct = f; fct(1);
    fct = g; fct(2.5);
}

You can see that fct will only accept int and afterwards compiler promote it to double, So In the output of fct(2.5); it will print 2 //g:double->int and not 2.5 //g:double->int

Upvotes: 1

Related Questions