Home » Community » Newbie corner » Value strange behaviour result in ValueTypeError
Value strange behaviour result in ValueTypeError [message #51791] |
Fri, 31 May 2019 01:44  |
 |
Xemuth
Messages: 310 Registered: August 2018 Location: France
|
Senior Member |
|
|
Hello Community,
look at this code :
#include <Core/Core.h>
using namespace Upp;
VectorMap<String,Value> ConfigurationType;
template <class T> bool testAddVector(String keyname, const T &t){
if(ConfigurationType.Add(keyname,Value(t))) //Add t to value
return true;
}
CONSOLE_APP_MAIN
{
String theString = "Hello world";
ConfigurationType.Add("test1",Value(theString)); //simply add value to vector
Cout() << (String)ConfigurationType[0].Get<String>()<<"\n"; //Show the result from casting back the value to String
testAddVector<String>("test2", theString); // add value from a function (Crash on it)
//It result on Upp::ValueTypeError exception
Cout() << (String)ConfigurationType[1].Get<String>()<<"\n"; //Code stop Before this point
}
It result on a crash i don't get why.
Can someone explain me ?
I have put test case file below.
Thanks in advance,
Best regard.
my github : https://github.com/Xemuth
|
|
|
Re: Value strange behaviour result in ValueTypeError [message #51796 is a reply to message #51791] |
Fri, 31 May 2019 23:30   |
Novo
Messages: 1140 Registered: December 2006
|
Senior Contributor |
|
|
Your code is throwing an exception.
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1 0x00007ffff7a42535 in __GI_abort () at abort.c:79
#2 0x00007ffff7e0b642 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff7e17186 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff7e171d1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff7e17405 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x00005555556109c4 in Upp::Value::GetSmall<long long> (this=0x7ffff7fc85b0) at /home/ssg/dvlp/cpp/upp/git/uppsrc/Core/Value.hpp:209
#7 0x00005555555ae241 in Upp::Value::GetOtherBool (this=0x7ffff7fc85b0) at /home/ssg/dvlp/cpp/upp/git/uppsrc/Core/Value.cpp:203
#8 0x000055555556ca7e in Upp::Value::operator bool (this=0x7ffff7fc85b0) at /home/ssg/dvlp/cpp/upp/git/uppsrc/Core/Value.h:221
#9 0x000055555556d1ed in testAddVector<Upp::String> (keyname=..., t=...)
at /home/ssg/dvlp/cpp/sergey/upp/ValueStrangeBehaviour/ValueStrangeBehaviour.cpp:7
#10 0x000055555556be96 in ConsoleMainFn_ () at /home/ssg/dvlp/cpp/sergey/upp/ValueStrangeBehaviour/ValueStrangeBehaviour.cpp:17
#11 0x000055555558d2dd in Upp::AppExecute__ (app=0x55555556bc76 <ConsoleMainFn_()>) at /home/ssg/dvlp/cpp/upp/git/uppsrc/Core/App.cpp:343
#12 0x000055555556bc69 in main (argc=1, argv=0x7fffffffdf18, envptr=0x7fffffffdf28)
at /home/ssg/dvlp/cpp/sergey/upp/ValueStrangeBehaviour/ValueStrangeBehaviour.cpp:12
This is happening in this statement:
if(ConfigurationType.Add()) //Add t to value
because you are adding a String, which will be returned by the method Add(), and you are trying to implicitly convert Value, containing this string, to bool in the if() statement.
Such conversion is impossible. This is why you are getting an exception.
Regards,
Novo
|
|
|
Re: Value strange behaviour result in ValueTypeError [message #51797 is a reply to message #51791] |
Sat, 01 June 2019 11:32   |
Oblivion
Messages: 828 Registered: August 2007
|
Experienced Contributor |
|
|
Hello Xemuth,
Novo is right.
You are implicitly trying to convert a string "Value" to bool, which is not possible that way.
VectorMap<String, String> ConfigurationType;
template <class T>
bool testAddVector(String keyname, const T& t)
{
ConfigurationType.Add(keyname, t); // You have implicitly tried to convert a string "Value" into a bool "Value" (hence the "Value" error)
return true;
}
ValueMap vmap;
void TestValueMap(Value key, Value val) // Just change the key's signature to String if you want to limit the key type...
{
vmap.Add(key, val);
}
CONSOLE_APP_MAIN
{
StdLogSetup(LOG_COUT|LOG_FILE);
String s = "world";
testAddVector("Hello", s);
DUMP(ConfigurationType.ToString());
// If possible, I suggest using a ValueMap.
TestValueMap("Hello", "World");
TestValueMap(1, "This is a test");
TestValueMap("This is a test too!", 2);
DUMP(vmap);
}
Best regards,
Oblivion
upp-components: https://github.com/ismail-yilmaz/upp-components
[Updated on: Sat, 01 June 2019 11:36] Report message to a moderator
|
|
|
|
Goto Forum:
Current Time: Mon Jan 25 22:05:54 CET 2021
Total time taken to generate the page: 0.01763 seconds
|