-3

I'm trying to build a translator which has Java as the source language and C++ as the target language. I'm having some difficulties because in C++ there is no use of static blocks like there is in Java

In Java grammar a static block is defined as follows:

StaticInitializer:

    static Block

Now the question is: how can I translate this Java construct into C++ in a simple way?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Furabio JZ4
  • 145
  • 1
  • 7
  • How to translate it depends on what you want to achieve with it. – StoryTeller - Unslander Monica Nov 20 '17 at 19:43
  • SO is not a code translation service. Please read a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn how can you achieve what you want. – Algirdas Preidžius Nov 20 '17 at 19:44
  • 3
    create a class / struct, and put that code into its constructor. Then have a static instance of it where you need it. – Coderino Javarino Nov 20 '17 at 19:44
  • 2
    Beware that static initialization order is unspecified across multiple translation units in c++. This can lead to all sorts of unexpected (and usually undefined) behavior. Java static initialization occurs when a class is first accessed, giving some measure of control on which the code may rely to behave properly. A direct and safe translation may not be possible without a large amount of work to simulate the behavior. – François Andrieux Nov 20 '17 at 19:53
  • This might (or might not) turn out to be useful: http://szelei.me/cpp-static-init-block – rici Nov 20 '17 at 21:57

2 Answers2

2

As has already been mentioned in the comments, the Java spec mandates that a static constructor should run right before the first time the class is used. Quick-and-dirty solutions, such as adding a static member whose constructor contains the given code, do not satisfy that requirement. So you'll need to control the order in which the initialization happens yourself rather than relying on C++ features to take care of that for you.

The easiest way to do that would be to add a static member variable to keep track of whether the class has been initialized and a static method to initialize it. The static method would run the code from the static initializer block if (and only if) the class has not been initialized yet. Afterwards it would set the member to true. Now you can insert calls to the static method right before any use of the class and it will behave as specified.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • Wouldn't it be as easy as to declare a local/static instance of the class and let the module `ctor` initializer take care of that? – Frank C. Nov 21 '17 at 22:37
  • @FrankC. That wouldn't fit the requirement that the block runs right before (and only if) the class is used for the the first time, would it? Or maybe I don't understand your suggestion. – sepp2k Nov 21 '17 at 22:40
  • I didn't get the 'if and only if' unless you are talking about one of the comments explaining java. A static member variable is just as well, either way it's adding something to achieve parity with java. – Frank C. Nov 22 '17 at 12:28
  • @FrankC. In Java a static block is executed right before the class is used for the first time. And when the class is never used, the block doesn't run at all (that's the "only if" part). Just adding a static member won't match this behaviour, so the generated program would not behave equivalently to the original source program. – sepp2k Nov 22 '17 at 12:57
0

I'm not quite sure it's such a good idea to perform such an immediate translation from Java to C++, but in case you actually need a Java-esque static block in C++, you can certainly get one, using the mechanism described in this answer of mine.

The use would be something like:

static_block {
    std::cout << "I'm in the static block!" << std::endl;
}
einpoklum
  • 118,144
  • 57
  • 340
  • 684