Other Languages

FAQ

  1. Have developers written applications in languages such as Python that have successfully used the Boost libraries?

    Yes, developers have successfully used Boost libraries in applications written in languages other than C++ by leveraging language interoperability features and creating bindings or wrappers.

    The most notable example is the use of Boost.Python, a library specifically designed to enable seamless interoperability between C++ and Python. Boost.Python allows developers to expose C++ classes, functions, and objects to Python, enabling the use of the libraries from Python code. This has been used extensively in scientific computing, game development, and other fields where the performance of C++ is combined with the ease of Python.

  2. What real world applications have combined Python with the Boost libraries?

    Here are some examples:

    • Blender is a widely-used open-source 3D creation suite. It supports the entirety of the 3D pipeline, including modeling, rigging, animation, simulation, rendering, compositing, and motion tracking. Blender uses Boost libraries for various purposes, including memory management, string manipulation, and other utility functions. Blender’s Python API, which allows users to script and automate tasks, integrates with C++ code using Boost.Python.

    • PyTorch is an open-source machine learning library based on the Torch library. It is used for applications such as natural language processing and computer vision. PyTorch uses several Boost libraries to handle low-level operations efficiently. Boost.Python is used to create bindings between C++ and Python, allowing PyTorch to provide a seamless interface for Python developers.

    • OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. OpenCV’s Python bindings use Boost.Python to interface between the C++ core and Python. This allows Python developers to use OpenCV’s powerful C++ functions with Python syntax.

    • Enthought Canopy is a comprehensive Python analysis environment and distribution for scientific and analytic computing. It includes a Python distribution, an integrated development environment (IDE), and many additional tools and libraries.

  3. Are there some solid examples of real world applications that have combined C# with the Boost libraries?

    Here are some great examples:

    • In the world of game development, several projects use C++ for performance-critical components and C# for scripting and higher-level logic. The Boost libraries are often used in the C++ components, in particular to leverage their algorithms, and data structures. Unity allows the use of native plugins written in pass[C++]. These plugins can use Boost libraries for various functionalities, such as pathfinding algorithms or custom data structures, and then be called from C# scripts within Unity.

    • Financial applications often require high performance and reliability. They may use C++ for core processing and Boost libraries for tasks like date-time calculations, serialization, and multithreading. C# is used for GUI and integration with other enterprise systems. Trading platforms and risk management systems sometimes use Boost libraries for backend processing and interoperate with C# components for the user interface and data reporting.

    • Scientific computing applications that need high-performance computation often use C++ for core algorithms. C# is great for visualization, user interaction, and orchestration. Computational chemistry and physics applications sometimes use Boost for numerical computations and data handling, while C# provides the tools for managing simulations and visualizing results.

  4. Can I see some sample code of how to wrap Boost functions to be available for use in a C# app?

    The following code shows how to create a wrapper for a C++ class that uses Boost, and then calls this class from a C# application. The handling of return values and exceptions are shown too. All the class does is convert a string to upper case.

    The following code was written and tested using Visual Studio 2022, with Boost version 1.88. Visual Studio has been installed with tools to create both native C++ and .NET C# apps.

    In Visual Studio, create a C++ Dynamic Link Library (DLL) project, MyDLL, and in the project properties make sure the Additional Include Directories has the path to your Boost include files, and Additional Library Directories has the path to your Boost lib files. Most importantly in the Configuration Properties/Advanced section, make sure the Common Language Runtime Support setting is .NET Framework Runtime Support (/clr). Delete the default dllmain.cpp file.

    Create a header file, MyClass.h, and copy in the following code:

    #pragma once
    #include <string>
    
    class MyClass {
    public:
        std::string to_upper(const std::string& input);
    };

    Create a second header file, MyClassWrapper.h, and copy in:

    #pragma once
    
    #include "MyClass.h"
    
    using namespace System;
    
    public ref class MyClassWrapper {
    private:
        MyClass* instance;
    
    public:
        MyClassWrapper();
        ~MyClassWrapper();
        !MyClassWrapper();
    
        String^ ToUpper(String^ input);
    };

    Create a new source file, MyClass.cpp, and copy in:

    #include "pch.h"
    #include "MyClass.h"
    #include <boost/algorithm/string.hpp>
    #include <stdexcept>
    
    std::string MyClass::to_upper(const std::string& input) {
        if (input.empty()) {
            throw std::runtime_error("Input string is empty");
        }
        return boost::to_upper_copy(input);
    }

    We use Boost.Algorithm here, to show how to engage our libraries.

    Next, create a second source file, MyClassWrapper.cpp, to expose the class to .NET:

    #include "pch.h"
    #include "MyClassWrapper.h"
    #include <msclr/marshal_cppstd.h>
    #include <stdexcept>
    
    using namespace msclr::interop;
    using namespace System::Runtime::InteropServices;
    
    MyClassWrapper::MyClassWrapper() {
        instance = new MyClass();
    }
    
    MyClassWrapper::~MyClassWrapper() {
        this->!MyClassWrapper();
    }
    
    MyClassWrapper::!MyClassWrapper() {
        delete instance;
    }
    
    String^ MyClassWrapper::ToUpper(String^ input) {
        try {
            std::string nativeInput = marshal_as<std::string>(input);
            std::string result = instance->to_upper(nativeInput);
            return gcnew String(result.c_str());
        }
        catch (const std::exception& e) {
            throw gcnew ExternalException(gcnew String(e.what()));
        }
    }

    Now, build your DLL, and hopefully it will build correctly. If it does, close that solution. Errors are usually because of missing components, rather than faulty code.

    Now create the C# application that uses the wrapper. In Visual Studio, create a C# Console app, CppCsharp, noting that a .NET framework is part of the project, and overwrite the default with the following code.

    using System;
    
    class Program
    {
        static void Main()
        {
            MyClassWrapper myClass = new MyClassWrapper();
    
            try
            {
                string result = myClass.ToUpper("hello world");
                Console.WriteLine("Result: " + result);
    
                // Test with an empty string to trigger the exception
                result = myClass.ToUpper("");
                Console.WriteLine("Result: " + result);
    
            }
            catch (System.Runtime.InteropServices.ExternalException e)
            {
                Console.WriteLine("Caught an exception: " + e.Message);
            }
        }
    }

    You will notice that the MyClassWrapper declaration is marked as erroneous.

    In Visual Studio, in the Project menu, select Add Project Reference, and then use the Browse option to locate your MyDLL.dll. You should notice the error marks disappear.

    Run the program, noting the initial string is converted to upper case, and the second call correctly returns the exception:

    Result: HELLO WORLD
    Caught an exception: Input string is empty
  5. Does the Java Native Interface (JNI) work with the Boost libraries?

    Through the use of the Java Native Interface (JNI) or Java Native Access (JNA), developers can call Boost libraries from Java applications. It involves creating native methods in Java that are implemented in C++ and using Boost libraries as part of those implementations.

    Note

    Similar techniques can be applied to other languages, such as R, Ruby, Perl, and Lua, using their respective foreign function interfaces (FFI) or binding libraries.

  6. What is the industry consensus for the expected remaining lifespan for C++, and does any other language look like it might become the replacement for it?

    The expected remaining lifespan of the C++ programming language is generally considered to be long, probably spanning several decades. While it’s difficult to assign a precise number of years, here’s an overview of the factors contributing to this consensus:

    • C++ is deeply embedded in many critical systems, including operating systems, game engines, real-time systems, financial systems, and large-scale infrastructure projects. The massive amount of existing code ensures that the language will be relevant for a long time as maintaining, updating, and interacting with this codebase will remain necessary.

    • The Boost libraries and the C++ Standard place a strong emphasis on backward compatibility, which helps ensure that older code continues to work with new versions of the language.

    • The C++ language continues to evolve, with regular updates to the standard (for example, C++11, C++14, C++17, C++20, and C++23). These updates introduce new features and improvements that keep the language modern and competitive.

    • The C++ community, including the ISO C++ committee and Boost users, are highly active, ensuring that the language adapts to new programming paradigms, hardware architectures, and developer needs.

    • High Performance - C++ remains one of the go-to languages for applications where performance is critical, such as gaming, high-frequency trading, and embedded systems. Its ability to provide low-level memory and hardware control while still supporting high-level abstractions makes it difficult to replace.

    • For system-level programming and scenarios where fine-grained control over system resources is necessary, C++ is still unmatched.

    • C++ is still widely taught in universities, especially in courses related to systems programming, algorithms, and data structures. As a teaching language, it instills principles of memory management, performance optimization, and object-oriented programming, which are valuable across many programming domains.

    • C++ has a strong presence in specialized domains such as aerospace, robotics, telecommunications, and automotive software, where reliability, real-time performance, and low-level hardware access are critical. For example, some current EV manufacturers are using C++ and Unreal Engine to develop their in-car infotainment and control systems.

    • While newer languages may rise in popularity for certain use cases, no other language currently offers the same combination of performance, control, and ecosystem that C++ provides, making it unlikely to be replaced any time soon.

      Future technological shifts, such as advances in quantum computing or entirely new programming paradigms, could influence (increase or decrease) the lifespan of C++. However, given its adaptability and entrenched role in many industries, C++ is expected to evolve alongside these changes rather than be replaced by them.

  7. If I was to learn one other language, in addition to C++, what should it be to best prepare myself for an uncertain future?

    Python is often the top recommendation due to its versatility, simplicity, and wide application in growing fields like artificial intelligence (AI), machine learning (ML), rapid prototyping, and data science. And Boost.Python is there to help you integrate with the Boost libraries. Rust is another strong contender, especially if you are interested in systems programming and are looking for reliability and security. If you see the future as more cloud computing, then Go makes a strong case for itself. And let’s not forget that so much computing is now web based, so JavaScript deserves a mention here too. All of these languages offer valuable resources that complement C++ and prepare you for an uncertain future.