-
May 23rd, 2014, 02:22 PM
#1
[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);
}
-
May 23rd, 2014, 02:51 PM
#2
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.
-
May 23rd, 2014, 04:30 PM
#3
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)
-
May 23rd, 2014, 04:31 PM
#4
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)
-
May 24th, 2014, 01:36 AM
#5
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.
-
May 24th, 2014, 08:11 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|