Skip to content
Aleksandr edited this page Jul 2, 2019 · 4 revisions

Core language features

#include <stdex/core.h>
#include <stdex/type_traits>

template<class T>
int some_function(T *val_ptr) stdex_noexcept /*yep, noexcept function*/
{
    // note that 2nd param of static_assert(STATIC_ASSERT) is not c-string
    static_assert(stdex::is_integral<T>::value == true, some_func_value_should_be_integral);
    STATIC_ASSERT(stdex::is_const<T>::value == false, some_func_value_should_not_be_constant);
    
    if(nullptr == val_ptr)
        return -1;
    
    (*val_ptr) /= T(2);
    return 0;     
}
 
int main() 
{
    float *fptr = nullptr;
    float fval = 1.f;

    fptr = &fval;

    int err = some_function(fptr);

    return err;    
}

Strings

stdex::to_string example usage

#include <iostream>
#include <stdex/string>
 
int main() 
{
    using stdex::to_string;
    using std::string;

    double f = 23.43;
    double f2 = 1e-9;
    double f3 = 1e40;
    double f4 = 1e-40;
    double f5 = 123456789;
    string f_str = to_string(f);
    stdex::string // string is also defined in stdex namespace
        f_str2 = to_string(f2); // Note: returns "0.000000"
    string f_str3 = to_string(f3); // Note: Does not return "1e+40".
    string f_str4 = stdex::to_string(f4); // Note: returns "0.000000"
    std::string f_str5 = stdex::to_string(f5);
    std::cout << "std::cout: " << f << '\n'
              << "to_string: " << f_str  << "\n\n"
              << "std::cout: " << f2 << '\n'
              << "to_string: " << f_str2 << "\n\n"
              << "std::cout: " << f3 << '\n'
              << "to_string: " << f_str3 << "\n\n"
              << "std::cout: " << f4 << '\n'
              << "to_string: " << f_str4 << "\n\n"
              << "std::cout: " << f5 << '\n'
              << "to_string: " << f_str5 << '\n';
}

Threads

#include <stdex/thread>
#include <stdex/mutex>
#include <stdex/condition_variable>
 
stdex::condition_variable g_Condition;
stdex::mutex g_Mutex;

void call()
{
    stdex::unique_lock<stdex::mutex> lock(g_Mutex);
    stdex::this_thread::sleep_for(stdex::chrono::seconds(5));
    stdex::notify_all_at_thread_exit(g_Condition, lock); 
    // lock is 'moved' there as standard requires and is no longer valid
}

int main()
{
    stdex::thread callThread(call);
    callThread.detach();
    stdex::unique_lock<stdex::mutex> lock(g_Mutex);
    g_Condition.wait(lock);

    return 0;
}
Clone this wiki locally