View Single Post
Old 16th December 2016, 17:39   #27  |  Link
feisty2
I'm Siri
 
feisty2's Avatar
 
Join Date: Oct 2012
Location: void
Posts: 2,633
Quote:
Originally Posted by Mystery Keeper View Post
One component of good coding is explicit intent. Your example is obscure. Does it even work? Isn't the structure declared in the scope of operator? Or does "auto" pull the declaration outside?
it works, test code, compiled and executed smoothly under vs2015
Code:
#include <cstdint>
#include <cstdlib>
#include <iostream>

auto operator""ElementsLUTTable(size_t length) {
	struct LUTTable final {
		int32_t *RGBToLab = nullptr;
		int32_t *LabToRGB = nullptr;
		LUTTable(size_t length = 0) {
			RGBToLab = new int32_t[length];
			LabToRGB = new int32_t[length];
		}
		LUTTable(const LUTTable &) = delete;
		LUTTable(LUTTable &&obj) {
			RGBToLab = obj.RGBToLab;
			LabToRGB = obj.LabToRGB;
			obj.RGBToLab = nullptr;
			obj.LabToRGB = nullptr;
		}
		auto &operator=(const LUTTable &) = delete;
		auto &operator=(LUTTable &&obj) {
			if (this != &obj) {
				RGBToLab = obj.RGBToLab;
				LabToRGB = obj.LabToRGB;
				obj.RGBToLab = nullptr;
				obj.LabToRGB = nullptr;
			}
			return *this;
		}
		~LUTTable() {
			delete[] RGBToLab;
			delete[] LabToRGB;
		}
	};
	return LUTTable(length);
}

auto table = 16777216ElementsLUTTable;

auto main()->int {
	table.LabToRGB[123] = 123456;
	std::cout << table.LabToRGB[123] << std::endl;
	system("pause");
}
Quote:
One component of good coding is explicit intent.
that's exactly what I'm trying to do.
Code:
auto value = 1.234;
auto val = 123;
such statements are legal for all basic (hardware supported) data types, but wouldn't work on user defined data types before C++11.

user defined literals (operator""xxx) have been introduced to C++ since C++11, which makes such stuff
Code:
auto cval = 3 + 1.123i; //operator""i
auto table = 16777216ElementsLUTTable; //operator""ElementsLUTTable
possible, and now you could make all your custom data types just as native as the basic data types long as you want to.
Quote:
Does it even work? Isn't the structure declared in the scope of operator? Or does "auto" pull the declaration outside?
it works because the move constructor (since C++11) has been defined.

Last edited by feisty2; 16th December 2016 at 17:41.
feisty2 is offline   Reply With Quote