Skip to content

[BUG] How to use std::function in pure cpp2 ? #711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jituderaps opened this issue Sep 29, 2023 · 2 comments · Fixed by #1183
Closed

[BUG] How to use std::function in pure cpp2 ? #711

jituderaps opened this issue Sep 29, 2023 · 2 comments · Fixed by #1183
Labels
bug Something isn't working

Comments

@jituderaps
Copy link

jituderaps commented Sep 29, 2023

not able to use std::function in pure cpp2.
problem link: https://cpp2.godbolt.org/z/e6acYboTY

#include <functional>

fun: (x: int) -> int = 2 * x;

main: () -> i32 = {
    p_fun1: std::function<:(x: int) -> int> =  fun&;    // doesn't work
    // p_fun2: std::function<int(int)> = fun&;              // works fine

    std::cout << p_fun1(3);
}

command line : -DCMAKE_CXX_FLAGS="-std=c++23 -pedantic-errors -Wall -Wextra -Wconversion -Werror=unused-result" -GNinja -Wno-dev

Expected result : 6
Actual result :

example.cpp2(6,26): error: missing ';' at end of declaration or '=' at start of initializer (at '<')
example.cpp2(5,19): error: ill-formed initializer (at '{')
example.cpp2(5,1): error: unexpected text at end of Cpp2 code section (at 'main')
example.cpp2(2,0): error: parse failed for section starting here

@jituderaps jituderaps added the bug Something isn't working label Sep 29, 2023
@jituderaps jituderaps changed the title [BUG] How to use std::function in cpp2 ? [BUG] How to use std::function in pure cpp2 ? Sep 29, 2023
@JohelEGP
Copy link
Contributor

This is #343.

@hsutter
Copy link
Owner

hsutter commented Jul 27, 2024

Thanks! Your original code is now working with PR #1183, except you have to remove the : because : always means declaring a new thing and here you're only using the type so no : is needed:

fun: (x: int) -> int = 2 * x;

main: () -> i32 = {
    p_fun1: std::function<(x: int) -> int> =  fun&;  // works with PR #1183
    std::cout << p_fun1(3);  // prints 6
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants