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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:41:33+00:00 2026-05-24T09:41:33+00:00

I have two classes test1 and test2: struct test1 { int r; test1() {}

  • 0

I have two classes test1 and test2:

struct test1
{
   int r;

   test1() {}
   test1(int value) : r(value) {}

   test1 foo() const;
};

struct test2
{
   int r;

   test2() {}
   test2(int value) : r(value) {}
};

template <typename t>
struct data_t
{
   static const t one;
};

template <> const test1 data_t<test1>::one  = test1(1);
template <> const test2 data_t<test2>::one  = test2(1);

then i created a function to do somthing:

template <typename t, const t& value>
t do_somthing()
{ return value; };

the action of do_something is straightforward, it returns a copy of value, so in main function:

int main()
{
   test1 r = do_somthing<test1, data_t<test1>::one>();
}

the problem happens when implementing test1::foo

test1 test1::foo() const
{ return do_somthing<test1, *this>(); }

the compiler stops with error:
'this' : can only be referenced inside non-static member functions

with *this it becomes test1 const& which acceptable as 2nd parameter, so why this error?

  • 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-24T09:41:35+00:00Added an answer on May 24, 2026 at 9:41 am

    When you call a template method with explicit mention of parameters like,

    do_somthing<test1, data_t<test1>::one>(); //(1) ok
    do_somthing<test1, *this>();  // (2) error
    

    then compiler expects that the explicit arguments should be compile time constants. In your (2)nd case, *this is not resolvable to a compile time constant, so you are getting the compiler error.

    Change the definition to below:

    template <typename t>
    t do_somthing(const t& value)
    { return value; };
    

    and now when you call as,

    do_somthing<test1>(*this);  // (2) ok
    

    it should work. Because, now const t& doesn’t need to be a compile time constant, even though it’s resolved at compile time.

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

Sidebar

Related Questions

I have two classes, and want to include a static instance of one class
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
I have two classes ( Test1 and Test2 ) that each have a property
I have two classes, Foo and Bar, that have constructors like this: class Foo
Let's say we have two classes, Foo and Foo Sub, each in a different
I have two classes, Test2 and Test3. Test2 has an attribute test3 that is
I have two classes that are almost identical, besides one method. The classes have
I'm trying to update one textfield from another class: I have two classes: @interface
Is it possible to have two step definition classes with first being in one
I have two classes that each need an instance of each other to function.

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.