Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) DecltypeExample2

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppDecltypeExample2/CppDecltypeExample2.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

 

 

 

 

 

./CppDecltypeExample2/main.cpp

 

#include <algorithm>
#include <cassert>

std::vector<int> TimesTwo1(const std::vector<int>& v)
{
  //Create an empty return vector
  std::remove_const<std::remove_reference<decltype(v)>::type>::type w;

  //Define the type of an element
  typedef decltype(w)::value_type Value;

  //Insert elements in return vector
  std::transform(
    v.begin(),v.end(),std::back_inserter(w),
    [](const Value a) { return a * 2; }
  );
  return w;
}

std::vector<int> TimesTwo2(const std::vector<int>& v)
{
  //Create a return type vector from the input vector
  auto w(v);

  //Define the type of an element
  typedef decltype(w)::value_type Value;

  //Modify elements in return vector
  std::for_each(w.begin(),w.end(),[](Value& a) { a *= 2; } );
  return w;
}

auto TimesTwo3(const std::vector<int>& v) -> decltype(std::remove_reference<decltype(v)>::type{})
{
  auto w(v);
  typedef decltype(w)::value_type Value;
  std::for_each(w.begin(),w.end(),[](Value& a) { a *= 2; } );
  return w;
}

template <class T>
auto TimesTwo4(const std::vector<T>& v) -> decltype(std::vector<T>{})
{
  auto w(v);
  std::for_each(w.begin(),w.end(),[](T& a) { a *= 2; } );
  return w;
}

template <class T>
std::vector<T> TimesTwo5(const std::vector<T>& v)
{
  auto w(v);
  std::for_each(w.begin(),w.end(),[](T& a) { a *= 2; } );
  return w;
}

int main()
{
  const std::vector<int> v { 1,2,3 };
  const std::vector<int> e { 2,4,6 };
  assert(TimesTwo1(v) == e);
  assert(TimesTwo2(v) == e);
  assert(TimesTwo3(v) == e);
  assert(TimesTwo4(v) == e);
  assert(TimesTwo5(v) == e);
}

 

 

 

 

 

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