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

Errors with rvalue references in C++11 mode #17

Closed
Luthaf opened this issue Apr 11, 2015 · 2 comments · Fixed by #18
Closed

Errors with rvalue references in C++11 mode #17

Luthaf opened this issue Apr 11, 2015 · 2 comments · Fixed by #18

Comments

@Luthaf
Copy link
Contributor

Luthaf commented Apr 11, 2015

The following code throw a NcBadId exception at the auto attr = wrap.file.getAtt("Conventions"); line.

#include <string>
#include <netcdf>

using namespace std;
using namespace netCDF;

class Wrapper {
public:
    Wrapper(string name, string mode) {
        if (mode == "r")
            file = NcFile(name, NcFile::read);
        else if (mode == "w")
            file = NcFile(name, NcFile::write);
    }
    NcFile file;
};

int main(int argc, char** argv) {
    Wrapper wrap("file.nc", "r");
    auto attr = wrap.file.getAtt("Conventions");
    return 0;
}

While when I use direct initialisation for the file member, everything works.

class Wrapper {
public:
     Wrapper(string name, string mode) : file(name, NcFile::read) {}
    NcFile file;
};

Maybe the library is using the default generated operator=(const NcFile&), and this one is not correct. My compiler is clang based on LLVM 3.5.

@Luthaf
Copy link
Contributor Author

Luthaf commented Apr 12, 2015

More investigation on that. It looks like it is not the operator=(const NcFile&) which one is missing, but the C++11 operator=(NcFile&&), which is generated by the compiler in that case.

The same issue appears with other constructs like :

NcFile file("file.nc", NcFile::read);
// No "Foo" var in file.nc, this should throw. The operator=(NcVar&&) is used
NcVar var = file.getVar("Foo"); 
if (var.isNull())
    cout << "Error !" << endl;  // This does NOT happen

var.getAtt("Bar"); // Boom ! NcBadId here

This usage is fine though :

NcFile file("file.nc", NcFile::read);
// No "Foo" var in file.nc, this should throw. The NcVar(NcVar&&) constructor is used
NcVar var(file.getVar("Foo")); 
if (var.isNull())
    cout << "Error !" << endl;  // This DOES happen

@Luthaf Luthaf changed the title Wrong affectation operator for NcFile Errors with rvalue references in C++11 mode Apr 12, 2015
@Luthaf
Copy link
Contributor Author

Luthaf commented Apr 12, 2015

Ok, I was wrong all long ... I was not using the latest version, and I ran into this bug. I will propose a PR adding open and close methods to get around this bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant