Function template won't work with string literals

Issue is that c-array is not copyable, so

Filter<char [6]>{ *this, val, "==" }; // Error

Your overload is correct, but Filter needs to reordered and defined before your operator== overload. The overload returning Filter<T> is dependent on T, and so in that case Filter's definition can be postponed. But when you return Filter<std::string> the compiler needs an actual definition of Filter up front.

#include <iostream>

template<typename T>
struct Filter;

struct Field
{
    Field(const std::string &val): name(val) { }
    Field(std::string &&val): name(std::move(val)) { }
    std::string name;

    template <std::size_t N> Filter<std::string> operator==(const char (&val) [N]);

    template <typename T>
    Filter<T> operator==(const T &val);
};

template<typename T>
struct Filter
{
    Field f;
    T val;
    std::string op;
};

template <typename T>
Filter<T> Field::operator==(const T &val)
{
    return Filter<T>{ *this, val, "==" };
}

template <std::size_t N>
Filter<std::string> Field::operator==(const char (&val) [N])
{
   return Filter<std::string>{ *this, std::string(val), "==" };
}

int main() {
  Field f1 { "field1" };
  Field f2 { "field1" };
  std::cout << (f1 == 1).val;
  std::cout << (f1 == "Hello").val;
}

Demo


What you can do is specialize Filter for the case when T gets deduced to a char[N]. Adding

template<std::size_t N>
struct Filter<char[N]>
{
    Field f;
    std::string val;
    std::string op;
};

Will cause Filter<T>{ *this, val, "==" } to call the above specialization and it will use a std::string to store val.


Since you have the C++17 tag, here is yet another option to solve this problem: deduction guides

#include <iostream>

template<typename T>
struct Filter;

struct Field
{
    Field(const std::string &val): name(val) { }
    Field(std::string &&val): name(std::move(val)) { }
    std::string name;

    // note the use of auto here
    template <typename T>
    auto operator==(const T &val);
};

template <typename T>
auto Field::operator==(const T &val)
{
    // do not use Filter<T> here, or the deduction guide won't kick in
    return Filter{ *this, val, "==" };
}

template<typename T>
struct Filter
{
    Field f;
    T val;
    std::string op;
};

// ------- Deduction Guides -----------
template<typename T>
Filter(Field, T, std::string) -> Filter<T>;

// will tell the compiler to create a Filter<string> with a c-array argument
template<std::size_t N>
Filter(Field, const char(&)[N], std::string) -> Filter<std::string>;
// ------------------------------------

int main() {
  Field f1 { "field1" };
  Field f2 { "field1" };
  std::cout << (f1 == 1).val;

  // creates a Filter<string> instead of trying to 
  // create a Filter<const char(&)[6]> due to the deduction guide
  std::cout << (f1 == "Hello").val;
}