G
Gerhard Menzl
I have spent the better part of three working days isolating a most
puzzling defect in my Windows Forms application. The minimum code
required to reproduce the problem is:
// StdMaxBug.h
#pragma once
#include <algorithm>
namespace StdMaxBug
{
public __gc struct S
{
static int const s = 8;
};
void works ()
{
int i = 6;
int j = S::s;
int m = std::max (i, j);
}
void doesnt ()
{
int i = 6;
int m = std::max (i, S::s);
}
void test ()
{
works ();
doesnt ();
}
};
Calling test() from anywhere within your minimum Windows Forms
application causes a System.MissingFieldException to be thrown. The
error message is
Field not found: ?.s.
Note that the code works as expected as long as the value of the
constant S::s is copied into a variable first. It is only when the
constant is passed directly to std::max (or std::min, for that matter)
that the exception occurs.
What is extremely irritating (and was mainly repsonsible for taking
three days) is that the debugger cannot be used to identify the problem
because the exception is thrown as soon as you try to step into
doesnt(). Setting a breakpoint at the beginning of the function doesn't
help either.
Is this yet another subtle Managed C++ quirk I have overlooked? Or does
the code generator/JIT compiler/whatever simply go haywire?
An authoritative answer would be most welcome.
Gerhard Menzl
puzzling defect in my Windows Forms application. The minimum code
required to reproduce the problem is:
// StdMaxBug.h
#pragma once
#include <algorithm>
namespace StdMaxBug
{
public __gc struct S
{
static int const s = 8;
};
void works ()
{
int i = 6;
int j = S::s;
int m = std::max (i, j);
}
void doesnt ()
{
int i = 6;
int m = std::max (i, S::s);
}
void test ()
{
works ();
doesnt ();
}
};
Calling test() from anywhere within your minimum Windows Forms
application causes a System.MissingFieldException to be thrown. The
error message is
Field not found: ?.s.
Note that the code works as expected as long as the value of the
constant S::s is copied into a variable first. It is only when the
constant is passed directly to std::max (or std::min, for that matter)
that the exception occurs.
What is extremely irritating (and was mainly repsonsible for taking
three days) is that the debugger cannot be used to identify the problem
because the exception is thrown as soon as you try to step into
doesnt(). Setting a breakpoint at the beginning of the function doesn't
help either.
Is this yet another subtle Managed C++ quirk I have overlooked? Or does
the code generator/JIT compiler/whatever simply go haywire?
An authoritative answer would be most welcome.
Gerhard Menzl