TMap::Copy() does not copy


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.22/02
Platform: Ubuntu 20.04 amd64
Compiler: GCC 9.3.0


The TMap::Copy() method seems to do nothing. The map content is not copied. However, the TMap::Clone() method correctly produces a clone of the object.
Here is a small sample code to reproduce the problem.
Please let me know if I am missing something or if you want me to report this bug somewhere else:

    TMap map, map_copy;
    auto *key = new TParameter<Int_t>("key", 1);
    auto *value = new TParameter<Int_t>("value", 2);
    map.Add(static_cast<TObject *>(key), static_cast<TObject *>(value));
    map.Copy(map_copy);
    auto *value_copy = map_copy.GetValue("key");
    // value_copy is nullptr

Thanks
Giorgio

Hi @LastStarDust ,
that might very well be the case, if I read the code correctly TMap uses TCollection::Clone but TObject::Copy. @pcanal @Axel is this something to fix?

Cheers,
Enrico

Part of this confusion stems from the large TObject hierarchy and the implementation of operators as member functions. If you look at the documentation of the function you tried to call, TObject::Copy(), it explains that this isn’t doing what you’d expect.

So in summary it behaves as designed 20 years ago, and the name is very confusing. We generally try to not fix the TObject behavior as a lot of code relies on the current behavior, and much of that is simply not worth addressing. You can for instance generally use std::map<int,int> instead of having to go through TMap and TParameter<Int_t>. Let us know if that causes any issues.

Thank you for the explanation. Actually I have already fixed the problem by manually copying TMap.

I agree that the standard containers makes more sense and are easier to use. I was using TMap because I was under the impression that it was more “compatible” with ROOT. But, probably TMap was created when c++ was still young and the standard library lacking. Nowadays it might have lost most of its raison d’etre.

Anyway, why not just reimplement Copy in TMap instead of modifying TObject::Copy?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.