Member-function definition doesn't see "using declaration" in cpp file - c++11

There is a round function in a project in a separate namespace:
namespace CMMN
{
inline long round (double x) { return long (x < 0. ? x-0.5 : x+0.5); }
}
Some class is declared in a *.h file and it's member functions are defined in a separate *.cpp file:
using CMMN::round;
long SOME_CLASS::MemberFunction()
{
return round(sqrt(m_SomeValue));
}
The problem is that compiler generates an error:
error C2668: 'CMMN::round' : ambiguous call to overloaded function
...\commdef.h(222): could be 'long CMMN::round(double)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\math.h(1241): or 'long double round(long double) throw()'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\math.h(1125): or 'float round(float) throw()'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\math.h(516): or 'double round(double)'
while trying to match the argument list '(double)'
I wonder why ussing declaration in the begining of the cpp file does
not affect memeber function definitions below?
Is there a way to make
using declaration be seen inside member function definition? Adding using declaration for each function definition inside is a decision, but it is almost equivalent to full-qualified round calls.
Thanks in advance.

It does affect it. The compiler is telling you that there are 4 candidate functions that it could call, and it doesn't know which one to call. One of those candidates is CMMN::round, which shows that the using declaration is being recognized.
You have to disambiguate which round() you intend for it to call.

using CMMN::round within a namespace only makes that symbol visible within said namespace; it does not affect binding preference.
You appear to have also included <cmath> or <math.h> somewhere, which also declare overloads of round.
You will have to either place using declarations within function definitions, or fully qualify their names where they are used.

Related

Find and replace all occurrence of a function definition

I want to delete all occurrences of a function definition.
I have a lot of member functions with the same prototype overloaded in multiple derived classes
For example,
void **********::func()
{
.
..
...
}
The above function definition are different in number of lines in each occurrence.
Is there a way to do this in visual studio? If not in notepad++ or some other editor?

Disable tail calls to a function in Visual Studio

How can I disable tail calls to a specific function in Visual Studio?
The reason I need this is because I have a function that breaks to the debugger that I use when an error occurs and I need to see what function called it.
I cannot change global optimization options because the project runs too slow without optimizations.
(I'm assuming you're writing in C, because you failed to indicate the language or give any code.)
One way to ensure that an optimizing C compiler doesn't convert a Tail-Recursive Call into a jump would be to call through a function pointer variable. Set the function pointer equal to the function you want to recursively call, declare it volatile to keep the optimizer from outsmarting you, and replace
return x * factorial(x-1);
with
static int (*volatile factorial_fp)(int) = factorial;
return x * (*factorial_fp)(x-1);

How to define a constexpr variable

I want to use a simple compile time constant for example like this:
double foo(double x) { return x + kConstDouble; }
Now I see at least the following ways to define that constant
namespace { static constexpr double kConstDouble = 5.0; }
namespace { constexpr double kConstDouble = 5.0; }
static constexpr double kConstDouble = 5.0;
constexpr double kConstDouble = 5.0;
Which is the right way to go? Is there a difference when kConstDouble is defined in a header vs a source file?
Using static or an anonymous namespace will cause the variable to have internal linkage; it will only be visible within the same translation unit. So if you use one of these within a .cpp file, you won't be able to use the variable anywhere else. This would be done typically if the constant is an implementation detail of that unit of code. If you want to expose it to other translation units, you'll need to put it in a header file. The typical way to do that would be to declare it static (or put it in anonymous namespace), since it is a trivial and constant variable. The other approach would be to declare it extern in the header, and define it in the .cpp to get a truly global variable (as opposed top one where actually every tu has its own copy).
Between static and anonymous namespace; well you don't need both first of all. They both do the same thing AFAIK. But I think it is more idiomatic at this point to use anonymous namespaces in cpp files, as they can be used to also give functions, classes, etc internal linkage. On the other hand, when you want to use it for making a variable globally available, it's more common to use static; I never use anonymous namespaces in header files as I find it misleading.

What's happened with CMutablePointer and CConstPointer in Xcode Beta3?

What's happened with CMutablePointer and CConstPointer in Xcode Beta3?
Code that successfully compiles in Beta2 fails with the errors:
Use of undeclared type 'CMutablePointer'
Use UnsafePointer and ConstUnsafePointer respectively.
From the Release Notes:
APIs imported from C that use C pointers are now imported with a much simpler API type
structure which is more predictable, preserves const mutability in more cases, and preserves
__autoreleased pointer information.  Now you will see UnsafePointer,
ConstUnsafePointer, AutoreleasingUnsafePointer, etc.  Function pointers are also
imported now, and can be referenced and passed around.  However, you cannot call a C
function pointer or convert a closure to C function pointer type.!

x64 va_list in Visual Studio 2005

I have a class non-static member function, and it has variable arguments, I'm compiling on Visual Studio 2005, with the 64-bit runtime, on 64-bit Windows.
void Class::Foo(void* ptr,...)
{
va_list args;
va_start(args,ptr);
float f=va_arg(args,float);
va_end(args)
}
I'm expecting a float, I pass a float to the function. But when I debug - I don't get the float I've passed. In fact - it's being received by the function as a 64-bit double! I have to do this:
double d=va_arg(args,double);
float f=(float)d;
Now I know Win64 likes to pass parameters in registers, and casts floats when it does this, shouldn't a va_list always be on the stack?
According to most references, I should have just a clean stack full of the passed parameters.
My question is: is this correct behaviour, or a bug? And if it's a bug, is it my bug, or Microsoft's?
I have the defines WIN64 and _M_AMD64, and WIN32 is undefined.
I don't have the C++ standard here, but it follows the C standard in this matter. C99, 6.5.2.2p7 says
If the expression that denotes the
called function has a type that does
include a prototype, the arguments are
implicitly converted, as if by
assignment, to the types of the
corresponding parameters, taking the
type of each parameter to be the
unqualified version of its declared
type. The ellipsis notation in a
function prototype declarator causes
argument type conversion to stop after
the last declared parameter. The
default argument promotions are
performed on trailing arguments.
So for your float argument, "default argument promotions" are performed.
These are defined in p6 as
If the expression that denotes the
called function has a type that does
not include a prototype, the integer
promotions are performed on each
argument, and arguments that have type
float are promoted to double. These
are called the default argument
promotions. [...]
So all floats are converted to double when being passed to an ellipse. VS apparently conforms in this respect, and the bug is in your code, which shouldn't use float in va_arg.
Looks like it is a VC++ x64 bug.
FIX: The va_arg function returns an incorrect value in a Visual C++ 2005 application

Resources