私は複数の算術型で動作するプロジェクトを開発しています。<cmath>の一部の機能がstdネームスペースにないのはなぜですか?
user_defined_arithmetic.h:
typedef double ArithmeticF; // The user chooses what type he
// wants to use to represent a real number
namespace arithmetic // and defines the functions related to that type
{
const ArithmeticF sin(const ArithmeticF& x);
const ArithmeticF cos(const ArithmeticF& x);
const ArithmeticF tan(const ArithmeticF& x);
...
}
私を悩まれている私は、このようなコードを使用するときにということである。
だから私は、ユーザー定義の算術型のための最低限の要件が定義されているヘッダは、作られました#include "user_defined_arithmetic.h"
void some_function()
{
using namespace arithmetic;
ArithmeticF lala(3);
sin(lala);
}
私は、コンパイラのエラーを取得:
error: call of overloaded 'sin(ArithmeticF&)' is ambiguous
candidates are:
double sin(double)
const ArithmeticF arithmetic::sin(const ArithmeticF&)
私はヘッダーを使用したことがなく、<cmath>
のみを使用しました。私はusing namespace std
をヘッダファイルに使用していません。
gcc 4.6。*を使用しています。私はあいまいな宣言を含むヘッダが何であるかをチェックし、それがあることが判明:私は<cmath>
が<math.h>
が含まれていることを、知っているが、それはで宣言を保護する必要があります
Prototype declarations for math functions; helper file for <math.h>.
...
:
mathcalls.h std名前空間。私は<cmath>
ヘッダに掘ると見つける:
cmath.h:
...
#include <math.h>
...
// Get rid of those macros defined in <math.h> in lieu of real functions.
#undef abs
#undef div
#undef acos
...
namespace std _GLIBCXX_VISIBILITY(default)
{
...
だから、名前空間stdは#include <math.h>
後を開始します。ここに何か間違っているのですか、何かを誤解しましたか?
いくつかの点では動作しません。言うまでもなく:算術型(整数型+ダブル+フロート)を使用している場合、それは通常、より効率的です(共通)を参照渡しよりも値渡しすることができます。特定のバージョンが必要な関数を呼び出すときは、 'using namespace X'を追加するのではなく、呼び出しを修飾します。あるいは、* usingディレクティブ*( 'using arithmetic :: sin')を使用することもできます。最後に、 'typedef'を編集して型を変更するというアプローチは、本当に悪い考えです。 –
@ DavidRodriguez-dribeas:ありがとう!どうか、私に別の解決策を教えてもらえますか?番号はカスタムタイプにすることができるので、私は参照渡しを使用しています。つまり、それは数キロバイトも大きくなる可能性があります。私は関数をインライン化し、インラインの中でstdの基本関数を使用すると害は起こらないことを期待していました。それとも?私にいくつかの提案をお願いしますか? –
@DavidRodriguez-dribeas:C++のアプローチは抽象クラスを宣言することですが、行列計算に使用するライブラリは、組み込み型を使用するとかなりの最適化を使用します。私はちょうどこの利点を失いたくはありませんでした –