brackets

So I'm wondering why i can't use normal bracket when initializing a constructor in my header file?

1
2
3
4
5
//works
  juce::ValueTree fullTree{"Fulltree"};
//Doesn't work
juce::ValueTree fullTree("Fulltree");
// In my cpp file the normal brackets do work 


Just trying to understand why this is :)
Show us the class definition.

If you don't define a constructor, then the { } notation directly initializes each variable in the order they are declared.

So your class most likely looks something like:
1
2
3
4
class ValueTree {
public:
  std::string name;
};


(Nitpick: You don't initialize a constructor. You call a constructor, which initializes variables.)
https://docs.juce.com/master/classValueTree.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

class MyAudioProcessorEditor  : public juce::AudioProcessorEditor, public juce::Slider::Listener, public juce::Button::Listener, public juce::ComboBox::Listener{
  
  public:
    
    MyAudioProcessorEditor (MyAudioProcessor&);
    ~MyAudioProcessorEditor() override;
    
    void paint (juce::Graphics&) override;
    void resized() override;
    void sliderValueChanged(juce::Slider *slider) override;
    void buttonClicked(juce::Button *button) override;
    void comboBoxChanged(juce::ComboBox *comboBox) override;
    void FillSettingsPanelWithValues();
    void WriteToSettingsXML();
    void ReadFromSettingsXML();
    juce::ValueTree fullTree{"Fulltree"};
//If i write: juce::ValueTree fullTree("Fulltree"); it doesnt work
    


So the question is why do brackets not work inside header and why do curly brackets do work. And why do normal brackets work outside of the header in my cpp file :D
Last edited on
Lets simplify:
1
2
3
4
5
6
7
8
9
class Sample {
    int x {42};
    int y(7); // error
};

int main()
{
  Sample s;
}

main.cpp:3:11: error: expected parameter declarator
    int y(7);
          ^
main.cpp:3:11: error: expected ')'
main.cpp:3:10: note: to match this '('
    int y(7);
         ^

https://en.cppreference.com/w/cpp/language/class#Member_specification

You have member-specification and parentheses () are ok only for member functions.
The {} are part of initializer for member variable within class definition.

In your "cpp file" you probably mean "within implementation of constructor" (or other function). That is a different scope and allows more initializer syntaxes.
Last edited on
clear, thank you for explaining!
In practical terms, the reason why you can't use () but can use {} in this context is just, that's how the syntax for C++ was laid out. They theoretically could have made it work in cases where it wasn't ambiguous with a function declaration, but it's just the way it is.
Ganado wrote:
They theoretically could have made it work in cases where it wasn't ambiguous with a function declaration, but it's just the way it is.

It's already the case elsewhere.

Outside any class or inside a function, does
 
std::string foo();
1) declare a function that takes no arguments and returns a std::string, or
2) does it declare a variable of type std::string that is initialized using the default constructor?

The answer is 1, a function declaration. The rule is that if it can be interpreted as a function declaration then it will be. This is known as the "most vexing parse". https://en.wikipedia.org/wiki/Most_vexing_parse
Last edited on
Yes, perhaps I should have said, "They could have made it work in cases where it isn't a function declaration."
Last edited on
1
2
3
4
5
6
class Sample {
    int x {42};
    int y(7); // error
};

int z(3);


y is direct initialization with params. This is allowed outside of a struct/class when it can't be parsed as a function declaration (as here). This is not allowed within a class/struct.

z is also direct initialisation with params. However as this is outside of a class/struct it compiles OK.

Simply (very!), anything that looks like a function declaration is tried to be parsed as a function declaration (most vexing parse). If that fails within a struct/class, then a compile error is generated. If that fails outside of a struct/class it is then tried to be parsed as an assignment. Having a value without a type specifier as a param cannot be a function declaration (which needs a type) - so fails in a struct/class but works outside!
Topic archived. No new replies allowed.