Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9206951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:17:08+00:00 2026-06-18T00:17:08+00:00

I am trying to define some custom input types using the new-style YamlCpp API.

  • 0

I am trying to define some custom input types using the new-style YamlCpp API. I have two questions. Let me first say that we are currently using the 0.3 style-api and everything works great. The code-appearance of the new API is about 10x better though so we wanted to move to it.

  1. Why does my current approach bork the compiler?
  2. Is yaml-cpp smart enough to handle nested templates. For example, if my parser knows how to enc/dec std::vectors, and then I define a custom handler for MyAwesomeType separately — can I ask it to enc/dec a std::vector<MyAwesomeType> and it will figure it out?

My code began by copying and pasting the example on the website (here) and then modifying that. My current implementation attempts to handle conversion of std::vector<std::pair<qint16,qint16>> — this does not compile. (fyi – qint16 is simply a cross-platform typedef for __int16)

    #include "yaml-cpp\yaml.h"
    #include <vector>
    #include "qglobal.h"
    
    namespace YAML {
       // a std::pair of qint16's
       template<>
       struct convert<std::pair<qint16,qint16>> {
          static Node encode( std::pair<qint16,qint16> const& rhs) {
             Node node;
             std::vector<qint16> newVec;
             newVec.push_back(rhs.first);
             newVec.push_back(rhs.second);
             node = newVec;
             return node;
          }
          static bool decode(Node const& node, std::pair<qint16,qint16> & rhs) {
             if(!node.IsSequence() || node.size() != 2) {
                return false;
             }
    
             rhs.first = node[0].as<qint16>();
             rhs.second = node[1].as<qint16>();
             return true;
          }
       };
    
       // a vector of std::pair of qint16's
       template<>
       struct convert<std::vector<std::pair<qint16,qint16>>> {
          static Node encode( std::vector<std::pair<qint16,qint16>> const& rhs) {
             Node node;
             for(auto pairIt=rhs.begin();pairIt!=rhs.end();++pairIt)
             {
                 node.push_back( *pairIt );
             }
             return node;
          }
          static bool decode(Node const& node, std::vector<std::pair<qint16,qint16>> & rhs) {
             if( !node.IsSequence() ) {
                return false;
             }
    
             for(int k=0;k<node.size();++k)
             {
                 rhs.push_back( node[k].as<std::pair<qint16,qint16>>() );
             }
             return true;
          }
       };
    }

Finally, I would like to then use this to call

    std::map<std::string, std::vector<std::pair<qint16, qint16>>> m_vectorOfPairs;
    if( doc["myPairs"] )
    {
        // key exists!
        for( YAML::const_iterator it=doc["myPairs"].begin();it!=doc["myPairs"].end();++it)
        {
            m_vectorOfPairs[it->first.as<std::string>()] = it->second.as<std::vector<std::pair<qint16,qint16>>>();
        }
    }

…based on input yaml which looks like this…

    myPairs:
      pairOne: [[4, 25], [48, 336]]
      pairTwo: [[4, 25], [57, 336]]
      pairThree: [[4, 25], [48, 336]]

The compiler error output from this is about 300 lines, so I will not post that here.
Platform: Visual Studio 2010 SP1 x64

Thanks for your help.

EDIT:
Here is the first of many errors I get, namely that it doesn’t even seem happy parsing a Node key with the new style… I’m adding this because what is causing this may be related to the other errors.
Code

    YAML::Node doc = YAML::LoadFile(buildFilenamePath(m_spectCameraShortName, relativePath).toStdString());
        if( doc["numPixels"] )
    //...

Causes

    1>ANONPATH\thirdparty\yaml-cpp\include\yaml-cpp/node/detail/impl.h(146): error C2734: 'lhs' : const object must be initialized if not extern
    1>          ANONPATH\thirdparty\yaml-cpp\include\yaml-cpp/node/detail/impl.h(96) : see reference to function template instantiation 'bool YAML::detail::node_data::equals<const char[10]>(YAML::detail::node &,T (&),YAML::detail::shared_memory_holder)' being compiled
    1>          with
    1>          [
    1>              T=const char [10]
    1>          ]
    1>          ANONPATH\thirdparty\yaml-cpp\include\yaml-cpp/node/detail/node_ref.h(52) : see reference to function template instantiation 'YAML::detail::node &YAML::detail::node_data::get<const char[10]>(Key (&),YAML::detail::shared_memory_holder)' being compiled
    1>          with
    1>          [
    1>              Key=const char [10]
    1>          ]
    1>          ANONPATH\thirdparty\yaml-cpp\include\yaml-cpp/node/detail/node.h(103) : see reference to function template instantiation 'YAML::detail::node &YAML::detail::node_ref::get<const char[10]>(Key (&),YAML::detail::shared_memory_holder)' being compiled
    1>          with
    1>          [
    1>              Key=const char [10]
    1>          ]
    1>          ANONPATH\thirdparty\yaml-cpp\include\yaml-cpp/node/impl.h(333) : see reference to function template instantiation 'YAML::detail::node &YAML::detail::node::get<const char[10]>(Key (&),YAML::detail::shared_memory_holder)' being compiled
    1>          with
    1>          [
    1>              Key=const char [10]
    1>          ]
    1>          MYCODEFILE.cpp(75) : see reference to function template instantiation 'YAML::Node YAML::Node::operator []<const char[10]>(Key (&))' being compiled
    1>          with
    1>          [
    1>              Key=const char [10]
    1>          ]

Note that the Line 75 mentioned in the error above is the

    if( doc["numPixels"] ) 

line
I’ve recompiled from source latest using Rev 573 of default branch and all these examples are from that code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:17:09+00:00Added an answer on June 18, 2026 at 12:17 am

    Yes, yaml-cpp should be able to handle nested templates (and so you shouldn’t need to define a conversion for std::vector<std::pair<qint16, qint16>> – and, this suggests that yaml-cpp should define a conversion for std::pair<T, U> in general (http://code.google.com/p/yaml-cpp/issues/detail?id=188)).

    And I’m not sure why the compiler doesn’t accept your code (it looks correct, and compiles fine on Linux for me).

    Do the tests compile at all? Apparently, 0.5.0 doesn’t work out of the box on Visual Studio (see http://code.google.com/p/yaml-cpp/issues/detail?id=182), and I haven’t had a chance to test or merge the fix yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to define a custom layout for my report for which I'm using
I am trying to make some batch operations for custom objects that I have
I'm trying to get some custom routing going on in Magento using the following
I do a lot of custom applications at work. I'm trying to define some
pretty new to using Qt. I have a custom widget that i need to
I am trying to define an abstract class that has operators to compare two
I'm trying to define some default behaviours for my jQuery Dialogs like the following:
I am trying to do some custom lightweight JSF component binding. In my bean
I'm creating a module that let's you define some extra options for categorys in
AM trying to autoselect some on checkbox input based on radio button selection ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.