+1 vote
asked by (240 points)

I'm reading code of the file spinone.h. What does "return IQIndexVal{};" mean? I also found in the file siteset.h, there's "return IQIndexVal{}" and "return IQTensor{}"? You defined IQTensor and IQIndexVal as the names of 2 classes. What does it mean by "return classname {}"? Thanks a lot.

1 Answer

0 votes
answered by (70.1k points)

Hi, so you can think of Classname{} as being identical to Classname(). It just makes a default-constructed object of type Classname. The {} versus () is sort of a minor difference in C++11 where the curly braces passed to constructors forbid certain type conversions. So using it for a default constructor versus parentheses really makes no difference.

The reason some functions have lines returning default constructed objects is that otherwise the compiler will generate a warning that certain pathways through the function may not return a value. In most cases the return statement will never be executed but it's just there to ensure that the function is correctly formed according to standard C++.


commented by (240 points)
Thanks, but this lead to another question to me: I learnt from some online tutorial that when we use "class" we usually create an object of the class. If we want to initialize it with default constructor we just declare it as: Classname objectname - no parentheses after the objectname. Is here the case that we use class without creating an instance (I mean Classname{})?

I found another example, you defined a class "Heisenberg" in the file heisenberg.h and use the class directly in the example file idmrg.cc as:
IQMPO H = Heisenberg(sites, {"Infinite=",true});
Is the above code the same as:
Heisenberg instancename(sites,  {"Infinite=",true});
IQMPO H = instancename.IQMPO();
Why can we use it in the simpler way you wrote?

And by the way, is the word "operator" in "operator IQMPO() { init_(); return H; }" a name of the class that you defined? Or is it defined by C++?

Thank you very much.
Welcome to ITensor Support Q&A, where you can ask questions and receive answers from other members of the community.

Formatting Tips:
  • To format code, indent by four spaces
  • To format inline LaTeX, surround it by @@ on both sides
  • To format LaTeX on its own line, surround it by $$ above and below
  • For LaTeX, it may be necessary to backslash-escape underscore characters to obtain proper formatting. So for example writing \sum\_i to represent a sum over i.
If you cannot register due to firewall issues (e.g. you cannot see the capcha box) please email Miles Stoudenmire to ask for an account.

To report ITensor bugs, please use the issue tracker.