1

I want to declare a function while setting the value of a function pointer.

A TypeScript equivalent:

let fp: () => void;

fp = () => {
  console.log("Hello from inline function declaration!");
};

I've tried looking online but I can't seem to find anything that is related to what I'm looking for. I don't think this is possible but if it was it would be very helpful for a project I'm working on. I know this may be a stupid question but I really would like to know the answer.

4
  • In C++ (or C) you must define your function before to be able to assign it Commented Nov 10, 2024 at 21:04
  • @Mike Actually, all you need to do is declare a function. The compiler doesn't care if the function actually exists or not. It is the linker that ultimately looks for the function. Commented Nov 10, 2024 at 21:06
  • 2
    I've tried looking online but I can't seem to find anything that is related to what I'm looking for. -- The real issue is using other computer languages as a model in writing C++ code. If you do this, you will have programs that are 1) Buggy, 2) Inefficient, or 3) look weird to a C++ programmer. This has no issues. Commented Nov 10, 2024 at 21:12
  • Typescript and C/C++ don't really have anything in common regarding how functions work. You're better off taking courses or reading books on C++ to get answers to those basic questions, rather than asking about one-off stuff. This is part of standard patterns for headers and translation units. Commented Nov 10, 2024 at 21:13

2 Answers 2

7

A lambda without state can be converted to a function pointer. You can even force the conversion by using the unary + operator, as in

auto lambda = []() { do_something(); };
auto function_ptr = +[]() { do_something(); };

but normal implicit casting also works.

So this is roughly what you want.

#include <iostream>

int main()
{
    void (*fp)() = nullptr;
    fp = []() {
        std::clog << "Hello from inline function declaration!\n";
    };
    fp();
}
Sign up to request clarification or add additional context in comments.

3 Comments

And the declaration extern void (*fp)(); is also redundant in C++, although it would usually be placed in a header to share it with other translation units that don't know about the definition in the following line.
You don't need the extern, and you can move the lambda into main(). That may be closer to the OP's use-case, eg: void (*fp)() = nullptr; ... int main() { fp = []() { ... }; ... fp(); }
@RemyLebeau Ah okay. I'm not familiar with typescript so it wasn't clear to me whether this was global / module scope or function scope.
5

std::function and a lambda expression will basically let you have this syntax

std::function<void()> fp;

fp = [](){ console.log("Hello from inline function declaration!"); };

std::function does use type erasure so there is an overhead cost over a raw function pointer but this will let you use stateful functors as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.