Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) DecltypeExample1

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppDecltypeExample1/CppDecltypeExample1.pro

 

include(../../ConsoleApplication.pri) #Or use the code below
# QT += core
# QT += gui
# greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# CONFIG   += console
# CONFIG   -= app_bundle
# TEMPLATE = app
# CONFIG(release, debug|release) {
#   DEFINES += NDEBUG NTRACE_BILDERBIKKEL
# }
# QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++
# unix {
#   QMAKE_CXXFLAGS += -Werror
# }

include(../../Libraries/Boost.pri) #Or use the code below
# win32 {
#   INCLUDEPATH += \
#     ../../Libraries/boost_1_54_0
# }

SOURCES += main.cpp

 

 

 

 

 

./CppDecltypeExample1/main.cpp

 

#include <cassert>
#include <initializer_list>
#include <stdexcept>

struct MyFirst
{
  MyFirst(const int x) : m_x{x} {}
  MyFirst(const std::initializer_list<int> x) : m_x{SetX(x)} {}
  const int m_x;
  private:
  static int SetX(const std::initializer_list<int> x)
  {
    if (x.size() != 1) throw std::logic_error("Need exactly one argument");
    return *x.begin();
  }
};

struct MySecond
{
  MySecond(const std::initializer_list<int> x) : m_x{SetX(x)} {}
  MySecond(const int x) : m_x{x} {}
  const int m_x;
  private:
  static int SetX(const std::initializer_list<int> x)
  {
    if (x.size() != 1) throw std::logic_error("Need exactly one argument");
    return *x.begin();
  }
};

struct MyThird
{
  MyThird(const std::initializer_list<int> x) : m_x{SetX(x)} {}
  MyThird(const int x) : m_x{x} {}
  const int m_x;
  private:
  static int SetX(const std::initializer_list<int> x)
  {
    if (x.size() != 1) throw std::logic_error("Need exactly one argument");
    return *x.begin();
  }
};

MyThird operator+(const MyFirst& a, const MySecond& b)
{
  return MyThird( a.m_x + b.m_x );
}

//Similar to
// * Stroustrup, B., The C++ programming language, 4th Edition,
//   section 6.3.6.3, 'The decltype() specifier', page 165
template<class T, class U>
auto Product(const T& a, const U& b) -> decltype(T{} + U{})
{
  return decltype(T{} + U{})(a.m_x * b.m_x);
}

int main()
{
  const MyFirst  x(2);
  const MySecond y(3);
  const MyThird sum(x + y); //operator+ works fine
  assert(sum.m_x == 5);
  const auto product(Product(x,y));
  assert(product.m_x == 6);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml