Consider this piece of code

class Example
{
};

class ExampleAccessor
{
public:
  struct ConstructionFlag
  {
    ConstructionFlag()
    {
    }

    ~ConstructionFlag()
    {
    }
  };

  explicit ExampleAccessor(Example&)
  {
  }

  ExampleAccessor(Example&, const ConstructionFlag&)
  {
  }
};

int main()
{
  ExampleAccessor accessor(ExampleAccessor::ConstructionFlag());
}

Which is the minimal example of something I found in our code tree today. This compiles without any errors or warnings (on my compilers gcc 4.4 on Linux and mingw 4.5.2 on Windows both C++98). Reading the code I find this to be odd because I would have expected to get a compiler error for a mismatch parameter type while trying constructing an ExampleAccessor object.

It turns out that the compiler actually treats line 30 as a function declaration and produces no code for it.

Anyone else come across this before?
Can we get the compiler to warn/error about this (I tried most of the warning switches I was aware of without effect)
Is there a solution?

Actually I think they answer to the lastq question might be yes use a static const object rather than a temporary.

Turns out there was a solution however use of a static function provide to be better than a static const object directly

class Example
{
};

class ExampleAccessor
{
public:
  struct _ConstructionFlag
  {
    _ConstructionFlag()
    {
    }

    ~_ConstructionFlag()
    {
    }
  };


  explicit ExampleAccessor(Example&)
  {
  }

  ExampleAccessor(Example&, const _ConstructionFlag&)
  {
  }

  static const _ConstructionFlag& ConstructionFlag()
  {
    static const _ConstructionFlag cf;
    return cf;
  }
};

int main()
{
  Example ex;
  ExampleAccessor accessor(ex, ExampleAccessor::ConstructionFlag());
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.