C++ support in mXparser – key assumptions

TO SUPPORT MY WORK, ORDER A COMMERCIAL LICENSE
THANK YOU!

The tutorial consists of more than 200 live examples from 50 sections given separately for JAVA, C# and C++. Each of the examples can be copied and run on your own environment. In addition, mXparser provides an extensive collection of over 500 built-in math functions, expressions and symbols. Familiarize yourself with the scope and the syntax. Live testing is the best way to learn. Good luck! 🙂

Tutorial Math Collection API spec Download

Key Information on C++ support

Starting from version v.6.1 Sagitara, mXparser fully supports native C++ (C++17 standard). The C++ code has been designed to be compatible with multiple compilers (including MSVC, LLVM/Clang, GNU/GCC), operating systems (Windows, Linux, Unix, macOS, MinGW, MSYS2, WSL, Cygwin), and hardware architectures (x86, x64/AMD64, ARM). mXparser for C++ is based on CMake and distributed via GitHub. To use mXparser in your project, add the following code to your CMakeLists.txt file.

include(FetchContent)
FetchContent_Declare(
   MathParserOrgMxParser
   GIT_REPOSITORY https://github.com/mariuszgromada/MathParser.org-mXparser.git
   GIT_TAG v.6.1.0
   SOURCE_SUBDIR CURRENT/cpp/lib
)
FetchContent_MakeAvailable(MathParserOrgMxParser)
target_link_libraries(YourExecutable MathParserOrgMxParser)

mXparser internally operates on Unicode UTF16, but accepts data in UTF8

Text processing in mXparser for JVM/.NET is fully based on Unicode (with in-memory representation typically in UTF16). The C++ version uses wchar_t and std::wstring with full support for standard types like char and std::string. The internal text representation is std::wstring, but mXparser provides constructors that accept input data in std::string or const char*, which automatically converts from UTF8 to UTF16.

mXparser manages memory automatically

In Java and C#, memory management is handled by JVM and .NET, so mXparser users don’t need to handle this themselves. In C++, the paradigm is different. However, to ensure maximum API consistency between C++ and JVM/.NET versions, mXparser for C++ is fully based on smart pointers (std::shared_ptr). This way, if you use mXparser in the recommended way, memory management will be handled for you automatically.

Recommended usage of mXparser for C++

#include “org/mariuszgromada/math/mxparser.hpp

While it’s technically possible to use dedicated #include files such as

#include "org/mariuszgromada/math/mxparser/Expression.hpp"

we strongly discourage this approach. C++ differs significantly from Java/C# in terms of object initialization order and static variables. Using

#include "org/mariuszgromada/math/mxparser.hpp"

ensures that the entire library is properly initialized. The namespaces brought by mxparser.hpp always appear with the prefix

org::mariuszgromada::math::mxparser

so there’s no risk of naming conflicts with your own elements.

The “Ptr” Suffix and std::shared_ptr

The Ptr suffix indicates that the element is of type std::shared_ptr, and specifically alias NamePtr indicates that the element is of type std::shared_ptr<Name> Examples:

StringPtr = std::shared_ptr<std::wstring>
ExpressionPtr = std::shared_ptr<Expression>
ArgumentPtr = std::shared_ptr<Argument>
ArgumentExtensionPtr = std::shared_ptr<ArgumentExtension>
ConstantPtr = std::shared_ptr<Constant>
FunctionPtr = std::shared_ptr<Function>
FunctionExtensionPtr = std::shared_ptr<FunctionExtension>
FunctionExtensionVariadicPtr = std::shared_ptr<FunctionExtensionVariadic>
RecursiveArgumentPtr = std::shared_ptr<RecursiveArgument>
PrimitiveElementPtr = std::shared_ptr<PrimitiveElement>
StringResourcesPtr = std::shared_ptr<StringResources>

Inline functions as object factories / constructors

Almost every mXparser object in C++ provides a global inline function that acts as a constructor, initializing the object and returning a std::shared_ptr smart pointer to manage it. Direct stack allocation of mXparser objects is not recommended due to potential improper behavior of the library. Proper usage examples are presented below:

//Java, C#: Expression e = new Expression(…);
ExpressionPtr e = new_Expression(…);

//Java, C#: Argument x = new Argument(…);
ArgumentPtr x = new_Argument(…);

//Java, C#: Constant a = new Constant(…);
ConstantPtr a = new_Constant(…);

//Java, C#: Function f = new Function(…);
FunctionPtr f = new_Function(…);

Console Output

Similarly to the JVM/.NET version, you can use methods such as mXparser.consolePrint… The equivalents for C++ are mXparser::consolePrint… Important – the system console may not be prepared to display unicode text, as a result there may be many empty characters (often happens in Windows). Proper preparation of the console is completely on your side, because these settings typically change global options. In some cases, instead of the mXparser::consolePrint… methods, it is worth using the macros mXparser_consolePrint… These situations are when the types of expressions passed as a parameter to the method cannot be determined during compilation (e.g. no overloads of the appropriate operators). In such a situation, the aforementioned macro will try to add some operations that do not change the result, but allow compilation to be performed.

Now you are ready to explore the full tutorial

Each section of the tutorial includes examples in Java, C#, and their C++ equivalents. I wish you successful learning and effective use of mXparser’s native support.

Tutorial Math Collection API spec Download

Nuget – Package Manager (C#, F#, Visual Basic, …)

Install-Package MathParser.org-mXparser -Version 6.1.0

Nuget – .NET CLI

dotnet add package MathParser.org-mXparser --version 6.1.0

Nuget – Package Reference

<PackageReference Include="MathParser.org-mXparser" Version="6.1.0"/>

Maven – Dependency (Java, Kotlin, Scala, Groovy, …)

<dependency>
<groupid>
org.mariuszgromada.math</groupid>
<artifactid>
MathParser.org-mXparser</artifactid>
<version>
6.1.0</version>
</dependency>

Maven – Gradle

implementation 'org.mariuszgromada.math:MathParser.org-mXparser:6.1.0'

CMake – Dependency / FetchContent (C++, MSVC, LLVM/Clang, GNU/GCC, MinGW, MSYS2, WSL, Windows, Linux, Unix, MacOS)

include(FetchContent)
FetchContent_Declare(
MathParserOrgMxParser
GIT_REPOSITORY
https://github.com/mariuszgromada/MathParser.org-mXparser.git
GIT_TAG
v.6.1.0
SOURCE_SUBDIR CURRENT/cpp/lib
)
FetchContent_MakeAvailable(
MathParserOrgMxParser)
target_link_libraries(YourExecutable
MathParserOrgMxParser)

GitHub

git clone https://github.com/mariuszgromada/MathParser.org-mXparser

OTHER DOWNLOAD OPTIONS

Download latest release – v.6.1.0 Sagitara: .NET bin onlyDownload latest release – v.6.1.0 Sagitara: JAVA bin onlyDownload latest release – v.6.1.0 Sagitara: bin + doc

NEWS FROM MATHPARSER.ORG
SOURCE CODE

Source code .zipSource code .tar.gz
View on GitHubMathSpace.pl

My other creative spaces

DONATION
Did you find the software useful?
Please consider donation 🙂
DONATE