0

I am trying to reuse a variable inside a yaml file which is then going to be read inside a c++ (visual studio). My attempt is not working at the moment. Here is my approach:

Inside yaml file:

testName: &./test.yaml 

Later in yaml file I am reusing the above variable like this:

varName: *testName

However, when I read the yaml file inside the c++, may variable varName is still being read as "+testName". I have googled quite a bit and this approach seems to be the standard one.

Is there something I need to change inside the c++ environement to read the reused variable correctly/appropriately?

ps: Big fan of stackoverflow, my first post here so, "Hello World!" :)

Multiple google suggested answers and standard approaches

  • What YAML library are you using and does it document that it supports anchors and aliases? – Botje Mar 23 '23 at 09:22
  • I am using yaml 1.2. Was that what you were asking? – user35939 Mar 23 '23 at 09:29
  • What C++ *library* are you using to parse the YAML file in C++? – Botje Mar 23 '23 at 09:30
  • i am using the linked https://github.com/DLR-RM/3DObjectTracking/blob/master/ICG/ code. I am new to c++ and yaml so I might not be able to understand your question completely, i think. – user35939 Mar 23 '23 at 09:43
  • That uses [`FileStorage` from OpenCV](https://github.com/DLR-RM/3DObjectTracking/blob/master/ICG/src/common.cpp#L181). A quick look into the [OpenCV YAML implementation](https://github.com/opencv/opencv/blob/35f1a90df7e5a9b3b275a74868759efd787a8c70/modules/core/src/persistence_yml.cpp#L332) shows that they don't support anchors & aliases (and a lot of other things; calling this YAML is basically false advertisement). Rolling a homebrew parser is almost always a bad idea, yet here we are. For you this means: Unless you get the OpenCV people to add support for aliases, you're out of luck. – flyx Mar 23 '23 at 10:05

1 Answers1

1

The correct syntax is

testName: &a ./test.yaml 
varName: *a

The anchor/alias name is completely independent from the key name in the mapping. You can use testName if you want:

testName: &testName ./test.yaml
varName: *testName
flyx
  • 35,506
  • 7
  • 89
  • 126
  • I tried this as well, but still inside c++ my varName is read as "*testName". I think inside the c++ the alias of the variable is not being read the way I want it. Thanks for your quick response though :) – user35939 Mar 23 '23 at 09:49
  • You seem to be using OpenCV which [only supports YAML 1.0](https://github.com/opencv/opencv/issues/5836). That is beyond ancient, but *should* support anchors & aliases because those were part of YAML 1.0. If it doesn't, you'll have to raise an issue with them. – flyx Mar 23 '23 at 09:55