Reputation: 1364
In the spirit of generic programming, I've created the following code:
#include <iostream>
#include <functional>
class Functor
{
public:
void operator()()
{
std::cout << "Functor operator called." << std::endl;
}
};
void Function()
{
std::cout << "Function called." << std::endl;
}
void Call( auto & fp )
{
static int i;
std::cout << "Unified calling..." << &i << std::endl;
fp();
}
int main( int argc, char ** argv )
{
Functor functor;
std::function< void() > function = Function;
std::cout << "Begin testing..." << std::endl;
Call( functor );
Call( function );
std::cout << "End testing." << std::endl;
return 0;
}
Compiled with: g++ main.cpp -std=c++14
output:
Begin testing...
Unified calling...0x100402080
Functor operator called.
Unified calling...0x100402090
Function called.
End testing.
From what I can tell by the address of static, this produces two different functions, so it seems to me like a template shorthand, of a sort. My instinct is that one function to maintain is better than multiple ones, however, besides being mindful of the non-shared static variables, am I missing something that might make this a poor choice, instead of multiple function definitions?
Upvotes: 3
Views: 111
Reputation: 385104
It's impossible, is the main flaw.
auto
just means type deduction at the point of initialisation. It is not an "any" type. So, using auto
in this way would mean your function would have to be a template. In C++17, this will be possible and indeed an auto
parameter will automatically make the function a template (personally I find this extremely confusing, but oh well). But for now, no.
Upvotes: -1
Reputation: 62563
Yes, there are. They are forbidden by current C++ standard.
void Call( auto & fp )
is a compilation error on standard-conforming compiler.
Upvotes: 8