Skip to content

Instantly share code, notes, and snippets.

@jaques-sam
Created October 30, 2018 16:40
Show Gist options
  • Save jaques-sam/b1ce50d4f74eee29983e31ba94ddf021 to your computer and use it in GitHub Desktop.
Save jaques-sam/b1ce50d4f74eee29983e31ba94ddf021 to your computer and use it in GitHub Desktop.
ReturnValue's implicit type conversions improvements
#include <QDebug>
#include "returnvalue/returnvalue.h"
enum T_Enum {
bla,
boem
};
struct Struct {
int i = 8;
};
ReturnValue<T_Enum> returnEnum()
{
return bla;
}
ReturnValue<Struct> returnStruct()
{
return Struct{8};
}
int main()
{
// Should work, just return value
T_Enum theEnum = returnEnum();
qDebug() << "Enum = " << int(theEnum);
const Struct& theStruct = returnStruct();
qDebug() << "Enum = " << int(theStruct.i);
// Working conditions on different constructors
const ReturnValue<int> var0 = { ReturnError::SUCCESS, "var 0" };
const ReturnValue<void> var1 = { ReturnError::SUCCESS, "var 1" };
const ReturnValue<QSharedPointer<Struct>> var2 = { ReturnError::SUCCESS, "var 2" };
const ReturnValue<int> var3 { ReturnError::SUCCESS, QStringLiteral("var 0") };
const ReturnValue<void> var4 { ReturnError::SUCCESS, QStringLiteral("var 1") };
const ReturnValue<QSharedPointer<Struct>> var5 { ReturnError::SUCCESS, QStringLiteral("var 2") };
const auto var6 = ReturnValue<int>(ReturnError::SUCCESS, "var 6");
const auto var7 = ReturnValue<void>(ReturnError::SUCCESS, "var 7");
const auto var8 = ReturnValue<QSharedPointer<Struct>>(ReturnError::SUCCESS, "var 8");
ReturnValue<int> var9 = ReturnError::SUCCESS; // Best, clear definition
ReturnValue<void> var10 = ReturnValue<void>(ReturnError::SUCCESS); // Should be avoided, double definition
auto var11 = ReturnValue<QSharedPointer<Struct>>(ReturnError::SUCCESS); // Works, but example with var9 looks better
var9 << QStringLiteral("var 9");
var10 << QStringLiteral("var 10");
var11 << QStringLiteral("var 11");
// Unexpected failure cases, but rather unimportant
{
ReturnValue<int> var12 = ReturnValue<int>(ReturnError::SUCCESS) << QStringLiteral("var 12"); // ERROR: no viable conversion from 'ReturnValueBase' to 'ReturnValue<int>'
ReturnValue<void> var13 = ReturnValue<void>(ReturnError::SUCCESS) << QStringLiteral("var 13"); // WORKS, allowed for ReturnValue<void> as constructor is not explicit
ReturnValue<QSharedPointer<Struct>> var14 = ReturnValue<QSharedPointer<Struct>>(ReturnError::SUCCESS) << QStringLiteral("var 14"); // ERROR: idem
}
// Test implicit conversions
ReturnValue<int> var15 = { ReturnError::SUCCESS, QStringLiteral("Test")}; // WORKS
ReturnValue<void> var16 = returnEnum(); // WORKS: accept because ReturnValue<void> doesn't carry a value!
ReturnValue<T_Enum> var17 = ReturnValue<void>(); // ERROR: no viable conversion from 'ReturnValue<void>' to 'ReturnValue<T_Enum>'
ReturnValue<int> var18 = returnEnum(); // ERROR: no viable conversion from 'ReturnValue<T_Enum>' to 'ReturnValue<int>'
ReturnValue<T_Enum> var19 = returnStruct(); // ERROR: no viable conversion from 'ReturnValue<Struct>' to 'ReturnValue<T_Enum>'
ReturnValue<QSharedPointer<Struct>> var20 = returnStruct(); // ERROR: no viable conversion from 'ReturnValue<Struct>' to 'ReturnValue<T_Enum>'
ReturnValue<T_Enum> var21 = returnEnum(); // WORKS, same type
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment