1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
import <format>;
import <iostream>;
import <string>;
import <tuple>;
import <utility>;
using std::cout;
using std::string;
template <typename T>
struct myObject {
T data;
string status;
const std::pair<T, string> info() const noexcept;
std::pair<T, string> info() noexcept;
};
// Make a std::pair from the data members of the myObject class
template <typename T>
const std::pair<T, string> myObject<T>::info() const noexcept
{
return std::pair<T, string>(std::make_pair(this->data, this->status + " const"));
}
template <typename T>
std::pair<T, string> myObject<T>::info() noexcept
{
return std::pair<T, string>(std::make_pair(this->data, this->status + " non-const"));
}
int main ()
{
myObject testObject1{ 42, "Int" };
myObject testObject2{ 3.1, "Float" };
myObject testObject3{ 300.12, "Double" };
const myObject testObject4{ "String", "String" };
auto myTObj1{ testObject4.info() };
auto myTObj2{ testObject2.info() };
const auto myTObj3{ testObject3.info() };
print("From pair Object 1: {:>7}, {:<16} {}\n", myTObj1.first, myTObj1.second, "- Should be const");
print("From pair Object 2: {:>7}, {:<16} {}\n", myTObj2.first, myTObj2.second, "- Should be non-const");
print("From pair Object 3: {:>7}, {:<16} {}\n", myTObj3.first, myTObj3.second, "- Why not const??");
}
| |