0

How could you tell the compiler that this is a function declaration as opposed to a data member:

typedef std::basic_ostringstream<char16_t> foostringstream;
class TextStream: public foostringstream {
    public:
        std::forward_list<TextInstruction> output();
};

The problem is that the compiler (VS2015.3) for some annoying reason is under the impression it's a variable.

Warpspace
  • 3,215
  • 3
  • 21
  • 24
  • How did you figure that VS consider it as variable? – Humam Helfawi Oct 12 '16 at 06:59
  • It spits out this: `warning C4820: 'Text::TextStream': '4' bytes padding added after data member 'Text::TextStream::output'` – Warpspace Oct 12 '16 at 07:00
  • 1
    @Warpspace: Can't reproduce in VS2015 Update 3 on the above snippet. Maybe there's something else involved? – AnT stands with Russia Oct 12 '16 at 07:04
  • Very less likely that MSVC will consider `output` as a variable. Just for the sake of it, can you try: `std::forward_list (output)();` (see the braces around). – iammilind Oct 12 '16 at 07:04
  • Can you post the complete example and the error message? I tried to compile with online-VC++ and it works: http://rextester.com/TOQK11380. Maybe they are not using the same VC++ version? – Jens Oct 12 '16 at 07:05
  • @AnT: You need to turn on padding warnings to see that error message, which is a symptom, not the problem – Warpspace Oct 12 '16 at 07:05
  • @iammilind: No dice – Warpspace Oct 12 '16 at 07:06
  • @Warpspace: I was able to define the function (separately) and call it. And it worked as usual. So, it is a function in my case. Of course, I don't know what `TextInstruction` is, so I just did `typedef int TextInstruction;`. Hard to say whather it matters or not. – AnT stands with Russia Oct 12 '16 at 07:08
  • 1
    Actually I see this now: "warning C4820: 'TextStream': '4' bytes padding added after data member 'TextStream::operator ='". So, there is something strange going on. – AnT stands with Russia Oct 12 '16 at 07:09
  • Oh! That must mean it's the error message that is at fault! – Warpspace Oct 12 '16 at 07:10
  • @Warpspace: Yes, it looks like a broken warning message. – AnT stands with Russia Oct 12 '16 at 07:11
  • Obviously "Data Member" must not mean what I thought. OK, @Ant, can you please post an answer saying that it's actually recognised as a function and I'll accept it – Warpspace Oct 12 '16 at 07:11
  • @Warpspace: It appears that some a problem of some kind is there. In my case I complains about `TextStream::operator =` and I cannot try to call it, since it is deleted by the stream. If in your case it complains about your `output` function, it would be interesting to know what happens if you attempt to call `output` in your context. – AnT stands with Russia Oct 12 '16 at 07:14
  • I think that's a reference to the defaulted copy constructor that's implicitly defined. It must just report the last function in the struct, since there are no variables. I'm starting to suspect it's the Virtual Function Table that's the real cause of this warning. – Warpspace Oct 12 '16 at 07:38
  • You inherit from std::basic_ostringstream which for sure has member variables. Maybe these are the reason for the warning? Although I would expect that MS makes sure that there STL classes don't create compiler warnings. – Jens Oct 12 '16 at 07:53
  • Have you read http://stackoverflow.com/questions/4001736/whats-up-with-the-thousands-of-warnings-in-standard-headers-in-msvc-wall and http://www.drdobbs.com/cpp/padding-and-rearranging-structure-member/240007649 – UKMonkey Oct 12 '16 at 08:08
  • @UKMonkey: I'm deliberately checking packing. The fact that a non-virtual function appeared to cause padding made it look like the function declaration was being confused with a variable declaration. – Warpspace Oct 12 '16 at 23:29

1 Answers1

0

The issue is that you're creating an empty class, it's not a compiler problem. Imagine you did something like

class Empty{}
int main() {
  Empty a,b;
  assert (&a == &b);
  cout << sizeof(Empty)
}

what would you expect the result to be?

The compiler always adds padding to ensure that &a != &b, which means that sizeof Empty will be > 0 (usually 1).

UKMonkey
  • 6,941
  • 3
  • 21
  • 30
  • The super class (`basic_ostringstream`) should not be empty, though. I later added a virtual function, causing a function table to be created, and the error message is the same. – Warpspace Oct 13 '16 at 00:15
  • Well, I know there's a whole load more padding added if you use virtual inheritance, but no idea why yet... I don't know if it's required in the standard yet, I suspect not though as microprocessors don't have quite the same requirements on their memory. – UKMonkey Oct 13 '16 at 00:23
  • A while ago I asked about the Virtual Function Table with regards to the standard, and was told the VFT is not in the standard at all, as it's possible that a compiler might not use one. It just so happens that all the major compilers use it. There's thus not going to be anything in the standard about padding to do with it. I do suspect that the real cause of the padding is the VFT, but if so, then the error message is extremely misleading – Warpspace Oct 14 '16 at 02:10