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

  • Home
  • SEARCH
  • 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 8200299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:23:51+00:00 2026-06-07T06:23:51+00:00

Warning : longish introduction ahead needed to explain the problem. The Named Template Argument

  • 0

Warning: longish introduction ahead needed to explain the problem. The Named Template Argument idiom first described in ch 16.1 of Vandevoorde and Josuttis can be conveniently written with the Boost.Parameter library

    #include <iostream>
    #include <typeinfo>
    #include <boost/parameter.hpp>
    #include <boost/static_assert.hpp>

    struct DefaultPolicy1 {};
    struct DefaultPolicy2 {};

    typedef boost::parameter::void_ DefaultSetter;

    BOOST_PARAMETER_TEMPLATE_KEYWORD(Policy1_is)
    BOOST_PARAMETER_TEMPLATE_KEYWORD(Policy2_is)

    typedef boost::parameter::parameters<
            boost::parameter::optional<tag::Policy1_is>,
            boost::parameter::optional<tag::Policy2_is>
    > PolicySelector;

    template
    <
            class PolicySetter1 = DefaultSetter,
            class PolicySetter2 = DefaultSetter
    >
    class BreadSlicer
    {
            typedef typename PolicySelector::bind<
                    PolicySetter1, 
                    PolicySetter2
            >::type Policies;

    public:
            // extract policies:
            typedef typename boost::parameter::value_type<
                    Policies, tag::Policy1_is, DefaultPolicy1
            >::type P1;

            typedef typename boost::parameter::value_type<
                    Policies, tag::Policy2_is, DefaultPolicy2
            >::type P2;
    };

The above code allows to override the optional template parameters of BreadSlicer in arbritrary order by naming them Policy1_is and Policy2_is. This makes it very convenient to to policy-based design with many defaulted parameters.

int main()
{
        typedef BreadSlicer<> B1;

        // can override any default policy
        typedef BreadSlicer< Policy1_is<int> > B2;
        typedef BreadSlicer< Policy2_is<char> > B3;

        // order of policy-setting is irrelevant
        typedef BreadSlicer< Policy1_is<int>, Policy2_is<char> > B4;
        typedef BreadSlicer< Policy2_is<char>, Policy1_is<int> > B5;

        // similar static asserts work for B1 ... B4     
        BOOST_STATIC_ASSERT((std::is_same<B5::P1, int >::value));
        BOOST_STATIC_ASSERT((std::is_same<B5::P2, char>::value));

        return 0;
}

In order to avoid very subtle ODR violations with policy-based design (for an explanation see this old post by Alexandrescu), I would like to be able to apply a CRTP pattern on the named template arguments:

    int main() 
    {
            // ERROR: this code does NOT compile!
            struct CuriousBreadSlicer
            :
                    BreadSlicer< Policy1_is<CuriousBreadSlicer> >
            {};

            typedef CuriousBreadSlicer B6;

            BOOST_STATIC_ASSERT((std::is_same<B6::P1, CuriousBreadSlicer>::value));
            BOOST_STATIC_ASSERT((std::is_same<B6::P2, DefaultPolicy2    >::value));

            return 0;
    }

However, the Boost.Parameter implementation above fails to compile because some internal static_assert fails with a message like (VC10 SP1)

‘main::CuriousBreadSlicer’ : an undefined class is not allowed as an
argument to compiler intrinsic type trait ‘__is_base_of’

Question: can this static checking be turned off? Either through a macro or a template trick?

As to possible work-arounds:

  1. The above code is functionally equivalent to this handwritten
    code
    . For that code, the CRTP pattern does work. However, it
    requires a lot of boiler-plate code that Boost.Parameter library so conveniently
    automates.
  2. I could require the CRTP parameter to always be first in the list of template
    arguments and not wrap it in a Policy1_is class. This solves the
    compile time errors, but it loses the order independence of overriding.

So it seems that I am what golf players call “in between clubs”. Which solution would be best?

  • 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-07T06:23:53+00:00Added an answer on June 7, 2026 at 6:23 am

    A minimal example with no CRTP:

    #include <boost/parameter.hpp>
    #include <boost/static_assert.hpp>
    
    BOOST_PARAMETER_TEMPLATE_KEYWORD(Policy1_is)
    BOOST_PARAMETER_TEMPLATE_KEYWORD(Policy2_is)
    
    typedef boost::parameter::parameters<
               boost::parameter::optional<tag::Policy1_is>,
               boost::parameter::optional<tag::Policy2_is>
            > PolicySelector;
    
    
    struct foo {};
    struct bar {};
    struct baz;
    typedef typename PolicySelector::bind<foo, baz>::type Policies;
    boost::parameter::value_type<Policies, tag::Policy1_is, bar>::type x; // <- !!!
    

    So boost::parameter::value_type requires the policy selector to be based on complete types, which is not the case for your handwritten classes.

    I’m not completely sure why a class would need itself as its own policy. If you need that, perhaps you could wrap an incomplete type in something complete:

    struct CuriousBreadSlicer : BreadSlicer <
              Policy1_is<CuriousBreadSlicer *> > // <- compiles
    

    Or you could use your own wrap_incomplete_type<> template, for clarity.

    When you use the policy, you can check if it’s wrapped, and unwrap it.

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

Sidebar

Related Questions

Warning: preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, 'info', to be a valid callback in [...]
warning: passing argument 1 of 'bsearch' makes pointer from integer without a cast and
Warning: I may have the wrong 'problem statement' but here it goes: A Campaign
(WARNING: this is my first java application, coming from .NET, so don't bash me
Warning: Invalid argument supplied for foreach() in /home/content/t/3/k/t3kmast3r/html/PushService.php on line 13 Warning: stream_socket_client() [function.stream-socket-client]:
Warning! This problem is not for the feint of heart. I've spent several days
Warning: This is my first 'real' WPF Application. I'm certain it's a logic error
Warning: Please treat me like the rookie I am. This is my first real
Warning: mysql_fetch_assoc() : supplied argument is not a valid MySQL result resource in *
Warning! Spoilers ahead! You are given the following information, but you may prefer to

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.