Skip to content
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

[BUG] std::move(variable) in ifstream >> in the generated cpp1 code not compiling #396

Open
HALL9kv0 opened this issue Apr 30, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@HALL9kv0
Copy link

Describe the bug
cpp2front makes the last usage of a variable be a std::move(variable). In the case the last usage of the variable is for reading a file using std::ifstream's ">> " operator, the generated cpp1 code doesn't compile and gives the following error (and about 10 screens of template errors):

error: no match for ‘operator>>’ (oper
and types are ‘std::ifstream’ {aka ‘std::basic_ifstream’} and ‘std::remove_reference
<std::__cxx11::basic_string&>::type’ {aka ‘std::__cxx11::basic_string’})
24 | read >> temp;
| ~~~~ ^~ ~~~~~
| | |
| | std::remove_reference<std::__cxx11::basic_string&>::t
ype {aka std::__cxx11::basic_string}
| std::ifstream {aka std::basic_ifstream}

To Reproduce
Steps to reproduce the behavior:

  1. Sample code - distilled down to minimal essentials please
read_file : () ={
read: std::ifstream = (file_name);
temp : std::string=();
read >> temp;
}
main: () ->int {
     read_file();
}
  1. Expected result - what you expected to happen
    Just the generated cpp1 function code:
    std::ifstream read {file_name}; 
    std::string temp {};
    read >> temp ;

  1. Actual result/error
    It should be a move in the general case, but for ifstream std::move() doesn't compile.
 std::ifstream read {file_name}; 
    std::string temp {};
    read >> std::move(temp);

Additional context
When I add temp+=temp; (just for a dummy last use of it) as the last line of the read_file() function, it generates the following code and works perfectly.

  std::ifstream read {file_name}; 
   std::string temp {};
   read >> temp ;
  temp += std::move(temp);

Should the std::move(variable) be in the >> operator? In the case of std::ifstream's >> it doesn't work. If we make an exception and not use std::move for >> in general, maybe other overloaded >> operators miss out.

@HALL9kv0 HALL9kv0 added the bug Something isn't working label Apr 30, 2023
@JohelEGP
Copy link
Contributor

See also #231.

@JohelEGP
Copy link
Contributor

JohelEGP commented Apr 30, 2023

(out x)* is a hack that works right now: https://cpp2.godbolt.org/z/WEo9s7MTn.
See also #400 (comment).

@JohelEGP
Copy link
Contributor

See https://github.com/hsutter/cppfront/wiki/Design-note%3A-Explicit-discard.
You should add _ = temp; as the last statement.

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
Development

No branches or pull requests

2 participants