2013-10-10 23 views
2

我有一个客户端试图编译过时的编译器,似乎没有std :: sin和std :: cos从C++ 11。 (和他们不能升级) 我正在寻找某种快速修复打入标题的顶部,以使std :: sin指向sin等。 我一直在尝试诸如罪的C++别名std ::罪 - 需要马虎快速修复

#ifndef std::sin 
something something 
namespace std{ 
point sin to outside sin 
point cos to outside cos 
}; 
#endif 

但我还没有运气

任何提示? 感谢

+0

'#定义std'可怕的,我知道。 – john

+0

@john,存在std名称空间,但sin和cos的C++ 11版本不在其中。它们只有旧版本,它们位于命名空间之外。 – user980058

+0

'std :: sin'和'std :: cos'不是从C++ 11开始的,它们从来都是C++的一部分。只要包括''而不是''。如果该评论是垃圾,那么请更具体地说明这个*“过时编译器”*缺乏的问题。 –

回答

3

原则,它应该工作使用

#include <math.h> 
namespace std { 
    using ::sin; 
    using ::cos; 
} 

其中的一些功能,但是,以一种有趣的方式实施,你可能需要使用类似这样的东西:

#include <math.h> 
namespace std { 
    inline float  sin(float f)  { return ::sinf(f); } 
    inline double  sin(double d)  { return ::sin(d); } 
    inline long double sin(long double ld) { return ::sinl(ld); } 
    inline float  cos(float f)  { return ::cosf(f); } 
    inline double  cos(double d)  { return ::cos(d); } 
    inline long double cos(long double ld) { return ::cosl(ld); } 
} 

请注意,这些方法都不是便携式的,它们可能也可能不起作用。另外,请注意,您无法测试std::sin被定义:您需要设置合适的宏名称。

+0

thx,我与后者一起去了,只是做了罪和cos的花车:) – user980058

+0

你不会在std命名空间中具有这些功能的编译器上发生多重定义错误吗? – Pete

2

一种选择是使用参考像这样的功能...

#include <math.h> 
namespace std 
{ 
    typedef double (&sinfunc)(double); 
    static const sinfunc sin = ::sin; 
} 
1

你不应该污染std命名空间,但以下可能的工作:

struct MYLIB_double { 
    double v_; 
    MYLIB_double (double v) : v_(v) {} 
}; 

namespace std { 
    inline double sin(MYLIB_double d) { 
     return sin(d.v_); 
    } 
} 

如果“sin” std下存在,这将直接调用的double参数。如果不是,则该值将被隐式转换为'MYLIB_double',并且将调用过载,其将在全局名称空间std或(因为std::sin(double)不存在)中调用sin。您可能需要为花车等

另一种可能是更好的建议是增加一个条件,他们可以使用重载:

#ifdef MYLIB_NO_STD_SIN 
namespace std { 
    inline double sin(double x) { 
     return ::sin(x); 
    } 
} 
#endif