CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    [RESOLVED] Template specialization does not work for user-defined object.

    Hello,

    Can you take a look why I am getting compile error.

    Thank you.


    Code:
    // clang++ main.cpp -g -std=c++11 -o main
    
    #include <iostream>
    
    class QuoteClass
    {
    public:
          QuoteClass() = default;
          QuoteClass(std::string param)  {symbol = param;}
          std::string get_symbol() {return symbol;}
    private:
          std::string symbol;
    };
    
    template<typename S, typename T1, typename T2>
    [[gnu::nothrow]]
    void assert_equals(S str, T1 p1, T2 p2)
    {
          if (p1 == p2)
          {
                std::cout << "Ok.";
          }
          else
          {
                std::cout << "Failed: " << p1 << " != " << p2 << ".";
          }
          std::cout << " " << str << std::endl;
    }
    
    template<>
    [[gnu::nothrow]]
    void assert_equals<std::string, QuoteClass, QuoteClass>(std::string str, QuoteClass p1, QuoteClass p2)
    {
          if ( (p1.get_symbol() == p2.get_symbol()) )
          {
                std::cout << "Ok.";
          }
          else
          {
                std::cout << "Failed.";
          }
          std::cout << " " << str << std::endl;
    }
    
    int main()
    {
          // It works for integers, other built-ins, and std::string.
          const int left_i = 10;
          const int right_i = 20;
          assert_equals("Test if integers are equal.", left_i, right_i);
    
          // Does not compile for user-defined object:
          // error: invalid operands to binary expression ('QuoteClass' and 'QuoteClass')
          const QuoteClass left_q("symbol");
          const QuoteClass right_q("symbol");
          assert_equals("Test if equal.", left_q, right_q);
    }

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Template specialization does not work for user-defined object.

    std::string get_symbol() const {return symbol;}

    void assert_equals<std::string, QuoteClass, QuoteClass>(const std::string &str, const QuoteClass &p1, const QuoteClass &p2)

    Or you could remove the specialization and implement operator==().

    gg
    Last edited by Codeplug; May 23rd, 2014 at 02:56 PM.

  3. #3
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    Re: Template specialization does not work for user-defined object.

    Thank you for responding but I get the same error. I'd like to use template specialization.

    main.cpp:17:14: error: invalid operands to binary expression ('QuoteClass' and 'QuoteClass')
    if (p1 == p2)

  4. #4
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    Re: Template specialization does not work for user-defined object.

    Thank you for responding but I get the same error. I'd like to use template specialization.

    main.cpp:17:14: error: invalid operands to binary expression ('QuoteClass' and 'QuoteClass')
    if (p1 == p2)

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Template specialization does not work for user-defined object.

    The problem is that when you write this:
    Code:
    assert_equals("Test if equal.", left_q, right_q);
    The type of the first argument is const char[15] (or char[15] that is not an lvalue). However, your specialisation has the first template parameter as std::string, so perhaps the compiler decided that the specialisation was not as good a match as the original function template. Therefore, you should write:
    Code:
    assert_equals(std::string("Test if equal."), left_q, right_q);
    Nonetheless, you should still declare get_symbol to be const, and you should use the constructor's initialiser list where appropriate.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    Re: Template specialization does not work for user-defined object.

    @laserlight -- that's was because of the string -- thanks a lot.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured