> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C++ Explicit Constructor

An explicit constructor in C++ is a constructor declared with the `explicit` specifier to prevent the compiler from performing implicit type conversions and copy-initialization. By default, any constructor that can be called with a single argument acts as a converting constructor, allowing the compiler to automatically convert the argument type to the class type. The `explicit` keyword disables this behavior, strictly mandating direct-initialization or explicit casting.

```cpp theme={"dark"}
class ClassName {
public:
    // Syntax: Place the 'explicit' keyword before the constructor declaration
    explicit ClassName(Type parameter);
};
```

## Mechanical Behavior

When a constructor is marked `explicit`, it alters how the compiler resolves object initialization. C++ distinguishes between direct-initialization and copy-initialization. An explicit constructor is only viable during direct-initialization.

* **Direct-initialization (Allowed):** Occurs when arguments are provided explicitly within parentheses `()` or braces `{}`.
* **Copy-initialization (Blocked):** Occurs when an object is initialized using the equals sign `=`, passed by value to a function, or returned by value from a function.

```cpp theme={"dark"}
class ImplicitData {
public:
    // Non-explicit constructor (Converting constructor)
    ImplicitData(int x) {}
};

class ExplicitData {
public:
    // Explicit constructor
    explicit ExplicitData(double y) {}
};

void processImplicit(ImplicitData d) {}
void processExplicit(ExplicitData d) {}

int main() {
    // --- Non-explicit behavior 
    ImplicitData d1(10);         // OK: Direct-initialization
    ImplicitData d2 = 10;        // OK: Copy-initialization (Implicit conversion)
    processImplicit(10);         // OK: Implicit conversion of int to ImplicitData

    // --- Explicit behavior 
    ExplicitData d3(5.5);        // OK: Direct-initialization
    
    // ExplicitData d4 = 5.5;    // ERROR: Copy-initialization is blocked
    // processExplicit(5.5);     // ERROR: Implicit conversion is blocked
    
    ExplicitData d5 = static_cast<ExplicitData>(5.5); // OK: Explicit cast forces conversion
    return 0;
}
```

## Multi-Argument Constructors (C++11 and later)

Prior to C++11, the `explicit` specifier was only meaningful for constructors callable with a single argument. With the introduction of uniform initialization (brace initialization) in C++11, constructors with multiple arguments can also participate in implicit conversions via list-initialization. Applying `explicit` to a multi-argument constructor prevents implicit list-initialization.

```cpp theme={"dark"}
class Point {
public:
    explicit Point(int x, int y) {}
};

void draw(Point p) {}

int main() {
    Point p1{1, 2};      // OK: Direct-list-initialization
    
    // Point p2 = {1, 2}; // ERROR: Copy-list-initialization blocked
    // draw({1, 2});      // ERROR: Implicit list-initialization blocked
    
    return 0;
}
```

## Conditional `explicit` (C++20)

C++20 introduced the conditionally explicit specifier, allowing the `explicit` keyword to take a boolean constant expression. This is primarily used in template metaprogramming to make a constructor explicit only if certain compile-time type traits are met.

```cpp theme={"dark"}
#include <type_traits>

template <typename T>
class Wrapper {
public:
    // Constructor is explicit only if T is not an integral type
    explicit(!std::is_integral_v<T>) Wrapper(T value) {}
};
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
