Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: C++ SFINAE : is_constructible for const char[] vs std::string

drus:

I'm trying to disable ctor that has non-std::string constructible type. My 1st attempt was like this :


#include

struct A
{
template ::value>::type>
A(U&& val)
{
std::cout (val)) }

template ::value>::type>
A(U&& val)
{
std::cout }
};

int main()
{
A a1(1);
A a2("hello");
A a3(std::string("hello"));
}

But compilation fails in line

A a1(1);

with the following error message :

error C2535: 'A::A(U &&)': member function already defined or declared (live example).

That means, that both conditions for SFINAE succeeded and both ctors are used.

I moved on and tried the following approach :


#include

struct A
{
template
A(U&& val, typename std::enable_if<:is_constructible u>::value>::type* = nullptr)
{
std::cout (val)) }

template
A(U&& val, typename std::enable_if::value>::type* = nullptr)
{
std::cout }
};

int main()
{
A a1(1);
A a2("hello");
A a3(std::string("hello"));
}

Luckily it compiles and works fine (live example).

So far, Im pretty fine with the 2nd solution but I don't really understand why the 1st approach with enabling/disabling ctor using templated parameter doesn't work.



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: C++ SFINAE : is_constructible for const char[] vs std::string

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×