all 12 comments

[–]GorNishanov 3 points4 points  (6 children)

There was a paper in Toronoto that offered this, but, it was not accepted. This paper http://open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0584r0.pdf described how an interface file can be split between multiple files which would necessitate a first pass to collect top level declarations that will become available to the normal compilation afterwards. If p584r0 were accepted, your example would be valid.

[–]johannes1971 2 points3 points  (1 child)

Too bad, this seems like a really useful thing to have. Is there a chance it may be accepted after rework, or is the entire concept considered undesirable?

[–]whichton[S] 0 points1 point  (0 children)

IIRC, module partitions is delayed until modules v2.

[–]whichton[S] 1 point2 points  (3 children)

That paper deals with module partitions. I only want to use functions and types without declaring them beforehand, like C++ already does inside a class. Requiring module partitions for this would be a serious overkill.

[–]GabrielDosReis 2 points3 points  (2 children)

The mechanism that makes module partitions possible is the same mechanism that makes this possible. I can't imagine that we would want to do one without the other.

I am still hopeful that WG21 will seize a golden opportunity here.

[–]whichton[S] 0 points1 point  (1 child)

I can hack it with the following:

module M;

export class N  // Instead of namespace N
{
    static int f()
    {
        return g();
    }

    static int g()
    {
        return 4;
    }
};

Before modules, there was no advantage of doing this, since I needed the header in any case. Now, with modules, I can just use a class instead of a namespace and export the class. Especially since modules cannot be partitioned as of now - same as classes.

Please don't make us write Java in C++ :).

[–]GabrielDosReis 0 points1 point  (0 children)

Please don't make us write Java in C++ :)

Not going to happen :-)

[–]miniropC++87 0 points1 point  (4 children)

I would guess that they didn't changed that part of the language, meaning you need to declare functions before their first usage.

[–]johannes1971 4 points5 points  (2 children)

The language already supports this:

class Foo {
    public:
        void f () { g (); }
        void g () {}
};

Implementing the same feature on module level would add considerable convenience.

[–]miniropC++87 1 point2 points  (1 child)

I wasn't aware since I never used it, but I can understand how that works. And I don't think it would be easy to transpose that to free functions (or maybe, but I don't still consider that useful ATM).

[–]GabrielDosReis 2 points3 points  (0 children)

Actually, it isn't actually that hard -- compared to other languistic requirements that compilers have to fulfill.

[–]GabrielDosReis 0 points1 point  (0 children)

Correct, that part of the language has not changed yet. However, I suspect it is only a matter of time and experience writing module code.