String 和 Int 之间的模板冲突

Template Conflict between String and Int

我有一个程序可以使用 json-glib 为 ReST Server 创建一个 JSON 文件。该文件有 2 个字段,即 idvalueid 只是 std::string 但值可以是整数、布尔值、字符串(通过 std::stringchar const *)或浮点数,具体取决于要传输的值.我在 c.str() 函数和 char *.

中遇到问题
if(std::is_integral<T>::value)
        {
            if(std::is_same<T, bool>::value)
            {
                if(json_builder_add_boolean_value (builder, tagValue) == nullptr)
                {
                    returnMessage = string("json_builder_add_boolean_value was inconsistent in setTag(Boolean). TagName : ") + tagName + string("TagValue : ") + to_string(tagValue);
    #ifdef __DEBUG
                   cerr << returnMessage;
    #endif 
                    return false;
                }
            }
            else
            {
                if(json_builder_add_int_value (builder, tagValue) == nullptr)
                {
                    returnMessage = string("json_builder_add_int_value was inconsistent in setTag(Int). TagName : ") + tagName + string("TagValue : ") + to_string(tagValue);
    #ifdef __DEBUG
                   cerr << returnMessage;
    #endif 
                    return false;
                }
            }
        }
        else if(std::is_floating_point<T>::value)
        {
            if(json_builder_add_double_value (builder, tagValue) == nullptr)
            {
                returnMessage = string("json_builder_add_double_value was inconsistent in setTag(Double). TagName : ") + tagName + string("TagValue : ") + to_string(tagValue);
    #ifdef __DEBUG
               cerr << returnMessage;
    #endif 
                return false;
            }
        }
        else if(std::is_same<T, string>::value or std::is_same<T, const string>::value)
        {
            if(json_builder_add_string_value (builder, tagValue.c_str()) == nullptr)
            {
                returnMessage = string("json_builder_add_string_value was inconsistent in setTag(String). TagName : ") + tagName + string("TagValue : ") + to_string(tagValue);
    #ifdef __DEBUG
               cerr << returnMessage;
    #endif 
                return false;
            }
        }
        else if(std::is_same<T, char *>::value or std::is_same<T, const char *>::value)
        {
            if(json_builder_add_string_value (builder, tagValue) == nullptr)
            {
                returnMessage = string("json_builder_add_string_value was inconsistent in setTag(String). TagName : ") + tagName + string("TagValue : ") + to_string(tagValue);
    #ifdef __DEBUG
               cerr << returnMessage;
    #endif 
                return false;
            }


}

error: request for member ‘c_str’ in ‘tagValue’, which is of non-class type ‘int’ if(json_builder_add_string_value (builder, tagValue.c_str()) == nullptr)

error: invalid conversion from ‘int’ to ‘const gchar* {aka const char*}’ [-fpermissive]

I'm using C++14

很遗憾。 您不能使用 if constexpr 这是您问题的自然解决方案,但它是从 C++17 引入的。

无论如何,如果没有if constexpr,编译器必须编译你函数的每一部分;因此,如果您的 tagValuebool,编译器还必须编译对 bool.

不可用的 tagValue.c_str() 调用

所以错误。

C++17 之前你必须为不同的类型开发不同的函数。

一种可能的解决方案是使用重载和 SFINAE 为三种确切类型(boolstd::string const &char const *)定义三个非模板 foo() 函数

void foo (std::string const & id, bool value)
 {
   // json_builder_add_boolean_value, etc

   std::cout << "-- bool case: " << id << ", " << value << std::endl;
 }

void foo (std::string const & id, std::string const & value)
 {
   // json_builder_add_string_value, etc

   std::cout << "-- std::string case: " << id << ", " << value << std::endl;
 }

void foo (std::string const & id, char const * value)
 {
   // json_builder_add_string_value, etc

   std::cout << "-- char * case: " << id << ", " << value << std::endl;
 }

和两个模板 foo() 函数,通过 SFINAE 为整数类型(第一个)和浮点类型(第二个)启用

template <typename T>
std::enable_if_t<std::is_integral<T>{}>
   foo (std::string const & id, T const & value)
 {
   // json_builder_add_int_value, etc

   std::cout << "-- integral case: " << id << ", " << value << std::endl;
 }

template <typename T>
std::enable_if_t<std::is_floating_point<T>{}>
   foo (std::string const & id, T const & value)
 {
   // json_builder_add_double_value, etc

   std::cout << "-- floating case: " << id << ", " << value << std::endl;
 }

所以打电话

   foo("1", false);
   foo("2", 0L);
   foo("3", 0.0f);
   foo("4", std::string{"zero"});
   foo("5", "zero");

你得到

-- bool case: 1, 0
-- integral case: 2, 0
-- floating case: 3, 0
-- std::string case: 4, zero
-- char * case: 5, zero

观察bool是整型,所以可以匹配foo()模板整型和foo()bool具体版本

在这种情况下,首选完全匹配,因此调用 bool 特定版本。