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 7067285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:11:00+00:00 2026-05-28T05:11:00+00:00

Given: struct Field { template<class T> T Value() const { // ??? } template<class

  • 0

Given:

struct Field
{
  template<class T> T Value() const
  {
    // ???
  }
  template<class S> S SimpleValue() const
  {
    return *reinterpret_cast<const S *>(GetVoidPointerToTheActualValue());
  }
  template<class P> const P *PointerValue() const
  {
    return reinterpret_cast<const P *>(GetVoidPointerToTheActualValue());
  }
};

How do I implement the Field::Value<T>() method, so that the compiler automatically dispatches to:

  • Field::PointerValue<P> if T is actually P*
  • Field::SimpleValue<S> otherwise

In addition it is guaranteed, that T is neither a reference nor a pointer-to-pointer type.

Thanks.

EDIT

@Grizzly – I have tried your suggestion, unfortunately it fails during the compilation of Value<LPCWSTR>():

1>  playmssqlce.cpp
1>c:\dev\internal\playmssqlce\playmssqlce.cpp(75): error C2668: 'sqlserver::Field::Value' : ambiguous call to overloaded function
1>          c:\dev\internal\playmssqlce\sqlserverfield.h(19): could be 'std::tr1::enable_if<_Test,_Type> sqlserver::Field::Value<LPCWSTR>(void) const'
1>          with
1>          [
1>              _Test=false,
1>              _Type=LPCWSTR
1>          ]
1>          c:\dev\internal\playmssqlce\sqlserverfield.h(18): or       'std::tr1::enable_if<_Test,_Type> sqlserver::Field::Value<LPCWSTR>(void) const'
1>          with
1>          [
1>              _Test=true,
1>              _Type=LPCWSTR
1>          ]
1>          while trying to match the argument list '(void)'

It is unclear to me why, because your advice feels right. BTW, I am using Visual Studio 2010.

EDIT2

After fixing the stupid mistake, I still have problems. So, here is what I have:

struct Field
{
  template<class T> typename enable_if<is_pointer<T>::value, T>::type Value() const { return PointerValue(); }
  template<class T> typename enable_if<!is_pointer<T>::value, T>::type Value() const { return SimpleValue(); }
  template<class T> T SimpleValue() const         { return *reinterpret_cast<const T *>(GetVoidPointerToTheActualValue()); }
  template<class T> const T *PointerValue() const { return reinterpret_cast<const T *>(GetVoidPointerToTheActualValue()); }
};

I am trying to compile f.Value<const wchar_t *>(), but getting this:

1>  playmssqlce.cpp
1>c:\dev\internal\playmssqlce\sqlserverfield.h(18): error C2783: 'const T *sqlserver::Field::PointerValue(void) const' : could not deduce template argument for 'T'
1>          c:\dev\internal\playmssqlce\sqlserverfield.h(42) : see declaration of 'sqlserver::Field::PointerValue'
1>          c:\dev\internal\playmssqlce\playmssqlce.cpp(75) : see reference to function template instantiation 'const wchar_t *sqlserver::Field::Value<const wchar_t*>(void) const' being compiled

What am I doing wrong now?

Thanks.

EDIT3

Stupid me. Noticed the change by Grizzly:

template<class T> typename enable_if<is_pointer<T>::value, T>::type Value() const { return PointerValue<typename std::remove_pointer<T>::type>(); }
template<class T> typename enable_if<!is_pointer<T>::value, T>::type Value() const { return SimpleValue<T>(); }

Works now.

  • 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-05-28T05:11:01+00:00Added an answer on May 28, 2026 at 5:11 am

    You can use enable_if:

    struct Field {
      /*Other methods of Field*/
      template<class T> 
      typename std::enable_if<std::is_pointer<T>::value, T>::type Value() const {
        return this->PointerValue<typename std::remove_pointer<T>::type>();
      }
      template<class T> 
      typename  std::enable_if<!std::is_pointer<T>::value, T>::type Value() const {
        return this->SimpleValue<T>();
      }
    
    };
    

    Of course std::enable_if, std::is_pointer<T> and std::remove_pointer<T> are only availible if you have C++11. If you don’t you can use either std::tr1::is_pointer or boost::is_pointer together with either boost::enable_if (or boost::enable_if_c) or a self written enable_if (look here for how to do it, it’s pretty trivial). remove_pointer is also availible as both std::tr1::remove_pointer and boost::remove_pointer.

    However depending on what you want it might still not do what you want, since the way I wrote it you need to pass const P* to Value(), since that is what PointerValue() returns. If you want to pass P* and get const P* back, you can change it to following:

    typename std::enable_if<
        std::is_pointer<T>::value, typename 
        std::add_const<typename 
            std::remove_pointer<T>::type
        >::type*
    >::type Value() const;  
    

    Once again use std::tr1::add_const or boost::add_const if you don’t have c++11

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

Sidebar

Related Questions

Given this class: class C { private: struct Foo { int key1, key2, value;
Given a struct, e.g. typedef struct { int value; } TestStruct; Why does the
Given the following classes: class Foo { struct BarBC { protected: BarBC(uint32_t aKey) :
Given two struct arrays A and B with field f1: A = struct('f1',{1,2,3}) B
Given a struct like this: public struct SomeStruct { public SomeStruct(String stringProperty, Int32 intProperty)
I've been given a struct for a 2D triangle with x and y coordinates,
Given a specific DateTime value, how do I display relative time, like: 2 hours
im working on a old code and i have this warning message: Passed-by-value struct
Given the following code: public struct Foo { public Foo(int bar, int baz) :
C language ensures that a pointer to any struct may be converted to void

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.