如何避免从整数 0 到指针的隐式转换,作为向量的元素

rus*_*yhu 11 c++ function-parameter implicit-conversion

有一种情况,我想收集 JSON 中一个键的路径的所有节点名称。考虑条件:JSON 数组索引“0”、“1”也是允许的,但是很容易忘记引号,这会导致在取消引用时崩溃。所以我希望编译器拒绝这种参数。例子:

#include <vector>
#include <iostream>

int func(const std::vector<const char*>& pin) {
    return pin.size();
}

int main() {
    // {"aname", "3", "path", "0"} wanted but this still compile
    std::cout << func({"aname", "3", "path", 0}) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

参考如何避免对非构造函数进行隐式转换?我尝试了以下内容:

#include <vector>
#include <iostream>

int func(const std::vector<const char*>& pin) {
    return pin.size();
}

int func(const std::vector<int>& pin) = delete;
// or
/* I want to describe only pointer type parameter is allowed as element,
but parameter of any integer types is rejected. */
template<typename T>
int func(const std::vector<T>& pin) = delete;

int main() {
    std::cout << func({"aname", "3", "path", 0}) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

但是编译器还是不能理解我。
有什么建议吗?

请指出任何滥用术语和假设的地方,谢谢!

Mik*_*ski 10

像这样的东西?它与您建议的重载解决方案非常相似,但需要包装向量类型。如果您提供文字,则无法构建,0因为选择了已删除的构造函数重载。

#include <memory>
#include <new>
#include <vector>
#include <iostream>
using std::vector;

template<typename T>
struct no_zero {
        no_zero(T val) : val(val) {}
        no_zero(int val) = delete;
        operator T() { return val; }
        T val;
};

int func(const vector<no_zero<const char*> >& pin) {
    return pin.size();
}

int main() {
    // {"aname", "3", "path", "0"} wanted but this still compile
    std::cout << func({"aname", "3", "path", 0}) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)