"static functions with block scope are illegal" error depending on initialisation style?
c++, initialization, scope, static
Solution
static Library::Transaction tt(Patron());
This is parsed as the declaration of a `static` (storage, not static member) function named `tt` that returns a `Library::Transaction` and takes a unnamed function as single argument. The argument function takes no values and returns a `Patron`.
Problem
I have a class `Library` containing a struct `Transaction`, and the struct has a member variable of type `Patron`. ``` class Patron { public: Patron() { } }; class Library { public: struct Transaction { Patron p; Transaction(Patron pp) :p(pp) { } Transaction(); }; }; ``` For the default constructor of `Transaction`, I have a function `default_transaction()` returning a const reference to a static object as recommended by Stroustrup in "Programming – Principles and Practices using C++" (p. 324); reasoning: avoid building default value into constructor code, avoid global variable and associated initialisation problems. ``` const Library::Transaction& default_transaction() { static Library::Transaction tt = Library::Transaction(Patron()); return tt; } Library::Transaction::Transaction() :p(default_transaction().p) { } int main() { Library::Transaction t; } ``` If I change this line ``` static Library::Transaction tt = Library::Transaction(Patron()); ``` to ``` static Library::Transaction tt(Patron()); ``` i.e., from "explicit" initialisation style to "colloquial", I get an error: "static functions with block scope are illegal". Why is that? Aren't the two equivalent? And what does the error mean exactly?